Having a hard time debugging error - Token '{' invalid key at column 2

后端 未结 4 1869
感动是毒
感动是毒 2020-12-16 09:18

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

form-field.html

4条回答
  •  被撕碎了的回忆
    2020-12-16 10:20

    Lets supppose this is my html

    Hi, it's {{name}}.
    The current time is .

    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: '',
            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.

提交回复
热议问题