in html,i bind id inside directive:
<my-customer data="id"></my-customer>
in 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'));
}
};
});
It can show data in template,but console shows undefined because the template hasn't been loaded with data,how i get the dom after data has been loaded? Thank you!
Using scope.$watch solve the problem:
in html:
<my-customer param="id"></my-customer>
in 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
});
}
};
});