【发布时间】:2015-09-30 03:56:28
【问题描述】:
我遇到了无法调试的错误。
form-field.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>
我收到以下错误:
在浏览器中:
错误:[$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
在角度网站上:
错误: $parse:syntax Syntax Error 语法错误: Token '{' invalid key 在表达式 [{{field}}.$error] 的第 2 列,从 [{field}}.$error].
有人知道为什么吗?谢谢!
【问题讨论】:
-
每当在 ng-class 和 ng-show 中引用时,您都需要将 {{field}} 更新为字段。
-
Nikhil - 在这个项目中它适用于他们:github.com/tutsplus/… 但不适用于我。
-
ng-show 属性不应包含 {{}}。试试
"field.$dirty"吧。 -
顺便说一句:这只是与您的问题无关,但是如果您将项目链接到未缩小的 Angular 源代码,您将在浏览器中收到可读的错误消息,而不是那些烦人的 URL 废话。 (我花了一年半的时间才发现这一点......)只是不要忘记在部署之前切换回缩小版本!
-
感谢丹尼尔的建议,我不知道缩小版本。现在我可以更轻松地找到我的问题了,谢谢!
标签: javascript angularjs angularjs-directive angularjs-scope