Angularjs指令,双向绑定不绑定

时间:2016-01-07 11:18:43

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

所有

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

scope: {
  dataSource: '='
}

链接功能内部:function(scope, ele, attr)

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

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

然而事实并非如此,这是一个例子:

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

由于

史蒂夫

1 个答案:

答案 0 :(得分:5)

将范围属性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

相关问题