【问题标题】:Angularjs directive, two way binding does not bindAngularjs指令,双向绑定不绑定
【发布时间】:2016-01-07 11:18:43
【问题描述】:

全部,

当涉及到指令时,我对 angularjs 的理解是,当您有这样的隔离范围设置时:

scope: {
  dataSource: '='
}

链接函数里面的那个:function(scope, ele, attr)

scope 将有一个dataSource 属性,如果像这样使用,它会绑定到我的控制器上的name

<my-element data-source='name'></my-element>

但事实并非如此,这里有一个例子:

http://jsfiddle.net/HB7LU/21879/

谢谢

史蒂夫

【问题讨论】:

    标签: angularjs angularjs-directive angularjs-scope isolate-scope


    【解决方案1】:

    将作用域属性的名称dataSource 更改为source。因为data 是保留名。

    var myApp = angular.module('myApp', []);
    
    //myApp.directive('myDirective', function() {});
    //myApp.factory('myService', function() {});
    
    myApp.controller('MyCtrl', function($scope) {
      $scope.name = 'Batman';
    })
    
    .directive('myElement', function() {
      return {
        template: '<div>Hello {{source}}</div>',
        replace: true,
        restrict: 'E',
        scope: {
                source: '='
        },
        link: function(scope, ele, attr) {
                console.log(scope.source);
        }
      }
    
    })
    

    Code Fiddle

    【讨论】:

    • 啊!谢谢,那些小问题。我不知道数据被保留。这是 javascript 还是 Angular 的东西?
    • 还有一件事rescrict: 'E' ,改成restrict:P
    猜你喜欢
    • 2015-03-07
    • 2019-07-23
    • 1970-01-01
    • 2018-04-22
    • 2015-10-29
    • 2015-03-26
    • 1970-01-01
    • 1970-01-01
    • 2013-06-15
    相关资源
    最近更新 更多