简体   繁体   English

如何将值传递给angularjs指令?

[英]How to pass values to an angularjs directive?

I have a directive that I want to pass a value, but it does not work: 我有一个要传递值的指令,但是它不起作用:

<p>{{testvar}}</p>
<my-fn info="{{testvar}}"></my-fn>

app.directive('myFn', function() {
    return {
        restrict: 'E',
        templateUrl: 'templates/my-fn.html',
        scope: {
            info: "=info"
        }
    };
});

my-fn.html: my-fn.html:

<p>the value is: {{info}}</p>

Result: The value in <p> tag is shown correctly, but the my-fn directive is not shown and prints the following error in the console: 结果: <p>标记中的值正确显示,但是my-fn指令未显示,并在控制台中输出以下错误:

Error: [$parse:syntax] http://errors.angularjs.org/1.4.3/$parse/syntax?p0=%7B&p1=invalid%20key&p2=2&p3=%7B%7Btestvar%7D
J/<@http://code.angularjs.org/1.4.3/angular.min.js:6:416

To answer my own question: one has to neglect the curly brakets as follows: 要回答我自己的问题:必须忽略以下几点:

<my-fn info="testvar"></my-fn>

If anyone knows why , please comment below. 如果有人知道为什么 ,请在下面评论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM