为什么不是这个领域我的指令绑定?
问题描述:
我有对dataSourceModel范围可变2路结合一个指令,但由于某种原因,它是示出了在指令为未定义。我在这里做错了什么?为什么不是这个领域我的指令绑定?
Plunker:http://plnkr.co/edit/LxWMbY9qtDSBUPWNqWV7?p=preview
代码:
HTML:
<div ng-controller='TestCtrl'>
<test-directive
selected-id='selectedId'
data-source-model='workOrderItems'> <!-- This does not work -->
</test-directive>
{{workOrderItems}} <!-- this works -->
</div>
脚本:
var app = angular.module("testApp", []);
app.controller('TestCtrl', ['$scope', function ($scope) {
$scope.workOrderItems = 'abcd';
$scope.selectedId = '123';
}]);
app.directive('testDirective',function() {
return {
restrict: 'E',
scope: {
selectedId: '=',
dataSourceModel: '='
},
replace: true,
template: "<div></div>",
link: function (scope, element, attrs) {
console.log(scope.selectedId, scope.dataSourceModel);
}
}
});
答
data-
是定制的HTML5属性前缀,所以data-source-model='workOrderItems'
被翻译成刚sourceModel
在你的指令中。
尝试重命名你的指令属性的东西,不以数据开始(或HTML引用它作为data-data-source-model
),它应该工作。
哇。现在很明显,你指出了...... – Scottie 2014-12-02 21:09:17