有些人可以解释为什么指令下面没有被调用?如果我直接将该指令添加到按钮,它会被调用。我尝试使用$超时添加,但仍然没有运气。为什么使用编程方式添加角度指令时不起作用
<html>
<head>
<title>AngularJS</title>
<script src = "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<script type="text/javascript">
var app = angular.module('MyApp',[]);
app.controller('MyCtrl',function($scope,$timeout){
document.getElementById('myBtn').setAttribute('my-directive','');
});
app.directive('myDirecitive',function(){
function linkFn (scope,element,attrs){
element.bind('click',function(){
alert('Clicked');
});
}
return {
restrict:'A',
link:linkFn
}
});
</script>
</head>
<body ng-app="MyApp", ng-controller="MyCtrl">
<button id="myBtn">Test</button>
</body>
</html>
指令名拼写错误 –
只是一个猜测,但你可能会想在阅读了[$范围$应用()](HTTPS:/ /docs.angularjs.org/api/ng/type/$rootScope.Scope#$apply)。 – adamdport
@adamdport我试过这样做$ apply。它仍然不起作用。 – Ambegodas