Angular指令1路绑定正在工作,但2路绑定不起作用

时间:2017-04-12 17:47:21

标签: angularjs angularjs-directive angularjs-scope angular-directive

  1. 我没有在控制器中使用示波器,因为我使用控制器等。
  2. 我有没有范围的掠夺者正在运作的概念证据
  3. 单向绑定正在运行
  4. 双向绑定不起作用 - 显示文字值
  5. HTML页面工作

     Here: {{detail.program.ldcCode}}  SHOWS   "Here: PNLC"
     <lcd-code code="{{detail.program.ldcCode}}"></lcd-code>
    

    上面将PNLC对象/值的单向绑定传递给Directive!

    指令:

     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: "@"以及{。1}} // PRINTS输出的console.log PNLC

    所以,当我切换到我急需的双向数据绑定时,这就是问题

    接下来是问题:

    从HTML传递到指令WITHOUT Expression

    console.log('ldcCode', attrs.code);

    指令

    <lcd-code code="detail.program.ldcCode"></lcd-code>
    

    ldcCode detail.program.ldcCode

    发生了什么事?

1 个答案:

答案 0 :(得分:0)

似乎attr链接函数参数显示了赋予属性的原始值。

Angular,当使用隔离范围和双向招标时,&#39; =&#39;运算符,获取该值并在父作用域上进行插值,以获取可通过链接函数上的scope参数访问的实际值。

参考Angular文档中的$compile.directive.Attributes

  

指令编译/链接函数之间的共享对象   包含规范化的DOM元素属性。值反映当前   绑定状态{{}}

如果你想得到属性的插值,即使不是在孤立的范围内,you can use the $observe method就可以了:

function linkingFn(scope, elm, attrs, ctrl) {
  // get the attribute value
  console.log(attrs.ngModel);

  // change the attribute
  attrs.$set('ngModel', 'new value');

  // observe changes to interpolated attribute
  attrs.$observe('ngModel', function(value) {
    console.log('ngModel has changed value to ' + value);
  });
}