首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >错误:$parse:syntax语法错误

错误:$parse:syntax语法错误
EN

Stack Overflow用户
提问于 2015-09-27 18:03:49
回答 3查看 4.1K关注 0票数 2

这个错误把我逼疯了,我需要帮助。

语法错误:从{ field }.$error开始的表达式{ field }}.$error列2处的令牌'{‘无效键。

form-field.html

代码语言:javascript
运行
复制
<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

代码语言:javascript
运行
复制
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

代码语言:javascript
运行
复制
<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>

留给我的,没有语法错误。知道为什么会这样吗?

EN

Stack Overflow用户

回答已采纳

发布于 2015-10-01 21:45:14

当计算{{ field }}.$error时,第一次{{ field }}还没有内插,因此角将第一个{作为对象声明的开始,第二个作为键。在第一个摘要周期之后,它可以工作,因为它已经被内插到whatever.$error中。

绝对没有必要使用{{ field }}。使用ng-form="form",将{{ field }}替换为form,并从remove()中移除参数(不使用双关语)。表单对象的名称完全不相关。

票数 1
EN
查看全部 3 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32811128

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档