这是我创建的指令:
HTML:
<p-test something="'bla'"></p-test>
JavaScript:
.directive('pTest', function() {
return {
scope: {
something: '=?'
},
templateUrl: 'components/testTemplate.html',
controller: 'testController'
};
});
我希望能够通过以下方式将“bla”作为不带“”的字符串传递:
<p-test something="bla"></p-test>
我知道可以通过链接中的 attributes 参数实现,但在这种情况下它是无关紧要的(如果我错了请纠正我),因为我将这些参数直接传递给范围。
最佳答案
I'd like to be able to pass 'bla' as a string without the '', in the following way:
为此,您只需要文本绑定(bind) (@
) 绑定(bind),而不是双向绑定(bind)。
.directive('pTest', function() {
return {
scope: {
something: '@?' //<-- Here
},
templateUrl: 'components/testTemplate.html',
controller: 'testController'
};
});
如果要绑定(bind)范围属性,则使用文本绑定(bind),然后使用插值。例如,如果 bla 是一个包含字符串的范围变量,那么只需执行以下操作:
<p-test something="{{bla}}"></p-test>
关于javascript - AngularJS:指令 - 无需使用引号即可传递字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27758841/