這裏是一個簡單的Angular JS示例的HTML和Javascript。在這個例子中,爲什麼沒有鏈接函數運行並將scope.flag
設置爲true
(boolean)而不是'true'
(字符串)?爲什麼我的AngularJS指令中的鏈接函數沒有運行?
使用Javascript:
angular.module('truthyTypes', [])
.directive('myDirective', function(){
return {
template: '<div>flag value: {{ flag }}<br/>flag type: {{ flag | typeOf }}</div>',
scope: {
flag: '@'
},
link: function(scope){
scope.flag = scope.flag === 'true';
}
}
})
.filter('typeOf', function(){
return function(input){
return typeof input;
}
})
;
HTML:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Example - example-example78-production</title>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.0-rc.1/angular.min.js"></script>
<script src="script.js"></script>
</head>
<body ng-app="truthyTypes">
<div my-directive flag="true"></div>
</body>
</html>
直播示例:http://plnkr.co/edit/jqk5529FmdGsxK6Xj8LZ?p=preview
的可能重複[AngularJS - 在指導的鏈接功能訪問隔離範圍(http://stackoverflow.com/questions/17111016/angularjs-access-isolated-scope-in-directives-link-function) – 2014-09-12 20:56:33