这个错误令我疯狂,我需要帮助。
Syntax Error: Token '{' invalid key at column 2 of the expression [{{ field }}.$error] starting at [{ field }}.$error].
形式,field.html
<div class='row form-group' ng-form="{{ field }}" ng-clase="{ '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-messages='required'> {{ field | labelCase }} is required. </p>
<p class='control-label' ng-repeat='(k, v) in types' ng-messages='{{ k }}'> {{ field | labelCase }} {{ v[1] }} </p>
</div>
</div>
directives.js
angular.module('ContactsApp')
.value('FieldTypes', {
text: ['Text', 'should be text'],
email: ['Email', 'should be email'],
number: ['Number', 'should be number'],
date: ['Date', 'should be date'],
datetime: ['Datetime', 'should be datetime'],
time: ['Time', 'should be time'],
month: ['Month', 'should be month'],
week: ['Week', 'should be week'],
url: ['URL', 'should be URL'],
tel: ['Phone Number', 'should be phone number'],
color: ['Color', 'should be color']
})
.directive('formField', function ($timeout, FieldTypes) {
return {
restrict: 'EA',
templateUrl: 'views/form-field.html',
replace: true,
scope: {
record: '=',
field: '@',
live: '@',
required: '@'
},
link: function ($scope, element, attr) {
$scope.types = FieldTypes;
$scope.remove = function (field) {
delete $scope.record[field];
$scope.blurUpdate();
};
$scope.blurUpdate = function() {
if ($scope.live !== 'false') {
$scope.record.$update(function (updatedRecord) {
$scope.record = updatedRecord;
});
}
};
var saveTimeout;
$scope.update = function() {
$timeout.cancel(saveTimeout);
saveTimeout = $timeout($scope.blurUpdate, 1000);
};
}
};
});
list.html
<p>
<input type='search' class='form-control' placeholder='Search' ng-model='query'/>
</p>
<table class='table table-hover table-bordered'>
<thead>
<tr>
<th ng-repeat='field in fields' ng-click='sort(field)'>
{{ field | labelCase }}
<span ng-show='sort.field === field && !sort.order' class='glyphicon glyphicon-chevron-down'></span>
<span ng-show='sort.field === field && sort.order' class='glyphicon glyphicon-chevron-up'></span>
</th>
</tr>
</thead>
<tbody>
<tr ng-click='show(contact.id)' ng-repeat='contact in contacts | filter: query | orderBy: sort.field : sort.order'>
<td ng-repeat='field in fields'>
{{ contact[field][0] }}
</td>
</tr>
</tbody>
</table>
留给我,有没有语法错误。任何想法为什么发生这种情况?
它的'NG-class'不NG-化酶:-) – Arkantos
你为什么需要动态地命名'ng-form'? –
我想,我强调一下,它与ng-message如何评估{field}。$错误有关。你想要做的是评估字段变量,添加“。$ error”然后评估整个表达式来确定你有什么错误。坦率地说,使用动态名称(字段)对我来说很陌生,如果{field}。$ dirty和{field}。$ invalid无法正确工作 – Vlad274