有些人可以解釋爲什麼指令下面沒有被調用?如果我直接將該指令添加到按鈕,它會被調用。我嘗試使用$超時添加,但仍然沒有運氣。爲什麼使用編程方式添加角度指令時不起作用
<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