javascript - 很难调试错误 - 第 2 列的 token '{' 无效 key

标签 javascript angularjs angularjs-directive angularjs-scope

我遇到了无法调试的错误。

表单字段.html

<div class='row form-group' ng-form="{{field}}" ng-class="{ 'has-error': {{field}}.$dirty && {{field}}.$invalid }">
    <label class='col-sm-2 control-label'> {{ field | labelCase }} <span ng-if='required'>*</span></label>
    <div class='col-sm-6' ng-switch='required'>

        <input ng-switch-when='true' ng-model='record[field][0]' type='{{record[field][1]}}' class='form-control' required ng-change='update()' ng-blur='blurUpdate()' />

        <div class='input-group' ng-switch-default>
            <input ng-model='record[field][0]' type='{{record[field][1]}}' class='form-control' ng-change='update()' ng-blur='blurUpdate()' />
            <span class='input-group-btn'>
                <button class='btn btn-default' ng-click='remove(field)'><span class='glyphicon glyphicon-remove-circle'></span></button> 
            </span>
        </div>
    </div>

    <div class='col-sm-4 has-error' ng-show='{{field}}.$dirty && {{field}}.$invalid' ng-messages='{{field}}.$error'>
        <p class='control-label' ng-message='required'> {{ field | labelCase }} is required. </p>
        <p class='control-label' ng-repeat='(k, v) in types' ng-message='{{k}}'> {{ field | labelCase }} {{v[1]}}</p>
    </div>
</div>

新.html

<h2> New Contact </h2>

<form name='newContact' novalidate class='form-horizontal'>
    <form-field record='contact' field='firstName' live='false' required='true'></form-field>



 <div class='row form-group'>
        <div class='col-sm-offset-2'>
            <button class='btn btn-primary' ng-click='save()'> Create Contact </button>
        </div>
    </div>
</form>

我收到以下错误:

在浏览器中:

Error: [$parse:syntax] http://errors.angularjs.org/1.4.1/$parse/syntax?p0=%7B&p1=invalid%20key&p2=2&p3=%7B%7Bfield%7D%7D.%24error&p4=%7Bfield%7D%7D.%24error

在 Angular 网站上:

Error: $parse:syntax Syntax Error Syntax Error: Token '{' invalid key at column 2 of the expression [{{field}}.$error] starting at [{field}}.$error].

有人知道为什么吗?谢谢!

最佳答案

我注意到在将数据绑定(bind)到自定义指令的属性时也会发生此错误。在哪里

$scope.myData.value = "Hello!";

这会导致错误:

<my-custom-directive my-attr="{{myData.value}}"></my-custom-directive>

但这工作正常:

<my-custom-directive my-attr="myData.value"></my-custom-directive>

关于javascript - 很难调试错误 - 第 2 列的 token '{' 无效 key ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31357055/

相关文章:

javascript - 地平线客户端库 : is group-by doable?

javascript - 如何在 Controller 中调用文件

javascript - ngMessages 的自定义包装可重用指令以最小化 HTML : Dynamic name to directive

javascript - 悬停时转换的 Angular 自定义指令显示错误

javascript - 在 $watch 中使用 $apply

AngularJS - 过滤数据时更新.length

javascript - Canvas + SVG 路径中的 Atan2 旋转卡顿

javascript - 防止div扩展滚动条

javascript - JQuery Click 事件使程序崩溃,没有控制台错误

angularjs - 如何在 Angular Material 设计中的工具提示中添加换行符