36

I have encountered an error which I'm unable to debug.

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>

new.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>

I'm getting the following error:

In the browser:

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

On angular site:

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

Does someone know why? Thanks!

Christian P
  • 12,032
  • 6
  • 60
  • 71
Angelo
  • 437
  • 1
  • 7
  • 9
  • You need to update {{field}} to field whenever referencing in ng-class and ng-show. – Nikhil Aggarwal Jul 11 '15 at 12:16
  • Nikhil - it works fine for them in this project: https://github.com/tutsplus/Building-a-Web-App-From-Scratch-With-AngularJS but not for me. – Angelo Jul 11 '15 at 12:33
  • 1
    ng-show attribute shouldn't have {{}} in it. Try just `"field.$dirty"` instead. – Daniel Beck Jul 11 '15 at 12:59
  • 2
    Incidentally: this is only tangentially related to your question but if you link your project to the unminified Angular source you'll get readable error messages in the browser instead of that irritating URL nonsense. (Took me a year and a half of annoyance before I discovered that...) just don't forget to switch back to the minified version before you deploy! – Daniel Beck Jul 11 '15 at 13:03
  • Thanks for the advice Daniel, I didnt know that about the minified version. Now Im able to locate my problems much easier thanks! – Angelo Jul 11 '15 at 14:49

4 Answers4

70

I notice that this error also happens when binding data to an attribute on a custom directive. Where

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

This causes the error:

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

But this works fine:

<my-custom-directive my-attr="myData.value"></my-custom-directive>
Daniel Flippance
  • 7,734
  • 5
  • 42
  • 55
  • 2
    your answer is an excellent tip. By the way, is there a free good debugger that can be plugged in google Chrome to catch that error? I am an AngularJs newbie. – Thomas.Benz Feb 22 '16 at 14:49
13

Your problem is here:

ng-class="{ 'has-error': {{field}}.$dirty && {{field}}.$invalid }"

Remove {{ }}:

ng-class="{ 'has-error': field.$dirty && field.$invalid }"

Also you have the same issue here:

ng-messages='{{field}}.$error'

Replace with:

ng-messages='field.$error'

However fixing those will most likely also cause an error for this line:

ng-message='{{k}}'

So you have to change it to:

ng-message='k'
Chrillewoodz
  • 27,055
  • 21
  • 92
  • 175
  • 'field' is a scope variable. I want to update html with that scope variable. If we remove {{}} then 'field' couldn't reflect in html page. Any solution for that ? – Jeff Johny May 17 '16 at 09:16
0

This problem happened to me when i was following the same tutorial , and i discovered that the solution in my case is that i was using a newer version of ngMessages so i have to update my bower.json file with the same version in the videos (starting from version 1.4 the example doesn't work), then every thing works fine and here is my dependencies section:

"dependencies": {
  "angular": "1.3.9",   
  "angular-route": "1.3.9", 
  "angular-resource": "1.3.9", 
  "angular-messages": "1.3.9", 
  "bootstrap": "^3.3.6"}
Ahmed Atalla
  • 48
  • 2
  • 3
0

Lets supppose this is my html

<div ng-controller='MyCtrl' ng-init="array=[{id:1}, {id:2}]">Hi, it's {{name}}. 
      <div ng-repeat='obj in array'>
        The current time is <display-time data="{{array}}"/>.
      </div>
</div>

Here display-time is the custom directive, whose definition is as follows

var demo = angular.module('demo', []);
demo.directive('displayTime', function($parse) {
    return {
        restrict: 'E',
        replace: true,
        scope: {
            data: '='
        },
        transclude: false,
        template: '<span class="currentTime"></span>',
        link: function (scope, element, attrs, controller) {
            var currentDate = new Date();
            console.log(scope.data);
            element.text(currentDate.toTimeString());
        }
    }});

Observe carefully, the syntax used for data="{{array}}".

Since i am using data in the scope of custom directive (with the statement

scope: {
    data: '='
},

),

i will get parse error

But if i use the syntax data="array", and i use the following code snippet inside the link function

scope: {
    //data: '='
},

then i will not get a parse error.

So you should use the syntax data="{{array}}" only if you want to access it as part of attrs parameter inside link function.

Deivide
  • 529
  • 8
  • 14
madhu sudhan
  • 177
  • 1
  • 14