我尝试动态渲染模板,如何在 {{tag}} 渲染完成后运行指令?
指令:
angular.module('services', [])
.directive('demo', function() {
return {
templateUrl: '/template.html',
restrict: 'C'
}
});
Controller :
angular.module('controller', [])
.controller('DemoCtrl', ['$scope', function($scope) {
$scope.tag = "demo";
$scope.clickHandler = function() {
// do something when click button
}
}]);
查看:
<button ng-click="clickHandler()">Button</button>
<div class="{{tag}}"></div>
最佳答案
您可以在指令
中使用以下结构:
app.directive('example', function() {
var directive : {
link:link,
restrict:'C',
templateUrl:'...'
}
function link($scope,$element,$attrs, $rootScope){
angular.element(document).ready(function() {
// dom here
})
}
return directive;
}
尝试一下,看看它是否适合您。
关于javascript - 如何在 Angularjs 中 dom 完成渲染后运行指令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25969234/