HTML 代码:
<body ng-controller="MainCtrl">
<h1>Hello {{name}}</h1>
<div id="aaa" myd1>
In a directive
</div>
<button ng-click="showDirectiveScope()">Show Directive Scope</button>
</body>
角代码:
var app = angular.module('angularjs-starter', []);
app.controller('MainCtrl', function($scope,$element) {
$scope.name = 'World';
$scope.showDirectiveScope = function() {
var aaa = $element.find("#aaa");
console.log(aaa);
console.log(angular.element(aaa).scope());
}
});
app.directive('myd1', function(){
return {
scope: true
}
});
页面中将有一个Show Directive Scope按钮。当我单击它时,我希望 angular 用 找到 DOM id=aaa
,然后获取并记录它由指令创建的范围myd1
。
但它会打印undefined
,哪里错了?