在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似乎不起作用。 –