【发布时间】:2013-03-09 02:05:43
【问题描述】:
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
}
});
页面中会有一个显示指令范围按钮。当我点击它时,我希望 Angular 找到带有id=aaa 的 DOM,然后获取并记录它由指令 myd1 创建的范围。
但是会打印undefined,哪里错了?
【问题讨论】:
标签: angularjs angularjs-directive