0
我在我的主控制器中有一个范围,然后在隔离范围内输入我的链接函数。有没有办法从我的链接功能访问主范围?这里是什么,我试图做一个简化的版本:在Angular中的链接函数中访问范围
角:
angular.module('root', [])
.controller('index', ['$scope', function($scope){
$scope.messages = ["Hello", "Howdy", "What's up"]
}
.directive('myDirective', function() {
return {
restrict: 'E',
scope: { greeting: '=' },
link: function(scope, element, attrs) {
var greeting = scope.greeting; //one message from the array, fed in by <my-directive greeting='message'> from index.html
var length = scope.messages.length; //length of whole messages array
//do stuff
}
}
}
HTML:(的index.html)
<body ng-app='root' ng-controller='index'>
<div ng-repeat='message in messages'>
<my-directive greeting='message'></my-directive>
</div>
</body>
我希望能够同时访问的消息和关于来自我的链接函数的整个消息数组的“元”信息。但是,现在我只能访问消息(var greeting),但是长度变量不计算。 有没有办法做到这一点在角?
让我知道你是否需要任何其他澄清!