2015-09-27 233 views
2

这个错误令我疯狂,我需要帮助。

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> 

留给我,有没有语法错误。任何想法为什么发生这种情况?

+0

它的'NG-class'不NG-化酶:-) – Arkantos

+0

你为什么需要动态地命名'ng-form'? –

+0

我想,我强调一下,它与ng-message如何评估{field}。$错误有关。你想要做的是评估字段变量,添加“。$ error”然后评估整个表达式来确定你有什么错误。坦率地说,使用动态名称(字段)对我来说很陌生,如果{field}。$ dirty和{field}。$ invalid无法正确工作 – Vlad274

回答

1

{{ field }}.$error评价首次{{ field }}尚未内插,所以角解释第一{作为对象声明的开始和所述第二一个作为密钥。在第一个摘要循环之后,它可以工作,因为它已被插入到whatever.$error

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

+0

我确实改变了你的要求,现在情况正在改善。但是我下次如何知道我必须使用'ng-form =“form”'而不是?那个是从哪里来的? – Emanuel

+0

@ siaw23'formField'指令具有隔离范围。这意味着它内部定义的所有东西都保留在它内部。 'ng-form =“无论”'意味着一个对象被放在该“孤立”范围内的'whatever'名下。因为每个'form-field'都有自己的''whatever'',所以不需要不同的名字。 – zeroflagL

+0

谢谢:)。我有一个问题,但。我是angularjs的新手。目前有1.4和2.0版本的角度。哪一个你会建议我花时间学习? – Emanuel

0

缺少一些东西,因为我没有看到该指令是从list.html实例化的,所以我们无法在这里得到整个图片。

考虑到这一点,您在form-field.html中使用{{field}}的方式很奇怪:指令接收field ='@',这意味着它是一个字符串,但在form-field.html中尝试将其用作对象。查看$ compile的文档,它解释了参数如何很好地传递到指令的隔离范围。

https://docs.angularjs.org/api/ng/service/ $编译

1

当你在你的html模板中用{{ }}包装代码时,它告诉角度它需要以角度表达式的形式运行它的内容。然而,如果你把它放在已经是一个角度表达式的东西里面,它会把它当作代码,并且抛出一个语法错误,因为它不是有效的javascript。以ng-开头的属性将被视为角度表达式。所以,你必须:

<div class='row form-group' ng-form="{{ field }}" ng-clase="{ 'has-error': {{ field }}.$dirty && {{ field }}.$invalid }"> 

它将对待ng-clase内容已经作为一个角度p21蛋白表达,所以{{ }}是不必要的。它应该是:

我已经与这个broken jsfiddle重现你的错误,并与此working jsfiddle固定它。

编辑:实际上,该行不是你的错误的原因,因为类型int ng-clase导致表达式被忽略。我认为这是导致你错误的路线是:

<div class='col-sm-4 has-error' ng-show='{{ field }}.$dirty && {{ field }}.$invalid' ng-messages='{{ field }}.$error'> 

可固定在一个类似的方式:

<div class='col-sm-4 has-error' ng-show='field.$dirty && field.$invalid' ng-messages='field.$error'> 
+0

'ng-form =“field”'会破坏'record [field] [0]'。 _“以ng开头的属性将被视为角度表达式”_不正确。这些属性也不会被内在地视为表达。 – zeroflagL