在HTML中,我绑定ID内部指令:如何让我绑定到模板向链路内的范围
<my-customer data="id"></my-customer>
在JS:
angular.module('Main', [])
.controller('Controller', ['$scope', function($scope) {
$scope.id= 'divId';
}])
.directive('myCustomer', function() {
return {
restrict: 'E',
scope: {
data: '='
},
template: '<div id="{{data}}"></div>',
link: function(scope,element,attrs){
console.log(document.getElementById('divId'));
}
};
});
它可以显示在模板数据,但控制台显示未定义,因为该模板尚未加载数据,数据加载后我如何获得dom? 谢谢!
使用范围$腕表解决的问题:
在HTML:
<my-customer param="id"></my-customer>
在JS
:
angular.module('Main', [])
.controller('Controller', ['$scope', function($scope) {
$scope.id= 'divId';
}])
.directive('myCustomer', function() {
return {
restrict: 'E',
scope: {
param: '='
},
template: '<div id="{{param}}"></div>',
link: function(scope,element,attrs){
scope.$watch('param',function(){
console.log(document.getElementById(param)); //get the dom
});
}
};
});
的可能的复制[AngularJS - 在指导的链接功能访问隔离范围(http://stackoverflow.com/questions/17111016/ angularjs-access-isolated-scope-in-directives-link-function) –
@RameshRajendran部分地,如果我将一个对象绑定到独立的作用域,$ attr似乎不起作用。 –