【发布时间】:2017-04-12 17:47:21
【问题描述】:
- 我没有在控制器中使用范围,因为我使用控制器等。
- 我有概念证明,没有范围的 plunker 正在工作
- 1 方式绑定工作
- 2 方式绑定不起作用 - 显示文字值
HTML 页面工作中
Here: {{detail.program.ldcCode}} SHOWS "Here: PNLC"
<lcd-code code="{{detail.program.ldcCode}}"></lcd-code>
上面以 1 方式将 PNLC 的对象/值绑定到指令!
指令:
return {
replace: true,
restrict: "EA",
scope: {
code: "@"
},
link: function (scope, element, attrs) {
console.log('ldcCode', attrs.code); // PRINTS out PNLC
因此,上述 1 方式绑定的工作原理是将 {{detail.program.ldcCode}} 作为表达式传入,然后在指令中将 code: "@" 与 console.log('ldcCode', attrs.code); 的 console.log 一起传递 // 打印出 PNLC
所以问题来了,当我切换到我急需的双向数据绑定时
接下来是问题:
在没有表达式的情况下从 HTML 传递到指令
<lcd-code code="detail.program.ldcCode"></lcd-code>
指令
scope: {
code : "="
},
link: function (scope, element, attrs) {
console.log('ldcCode', attrs.code);
LITERALLY this prints to chrome dev console as below in bold
ldcCode detail.program.ldcCode
发生了什么事?
【问题讨论】:
-
所以它的行为就像是一个字符串绑定 (@)。好像有些东西没有得到正确的保存。尝试单向箭头 (
-
不要在 Angular 表达式中使用
{{ }}插值。它们在属性@绑定中存在问题。不要使用双向=绑定。 -
那么我做错了什么,不要做 {{}} 并且 = 不起作用
-
双向
=绑定有效。人们每天都在使用它。 -
哦,我需要使用 scope.code 代替 attrs.code 吗?
标签: angularjs angularjs-directive angularjs-scope angular-directive