2
我有一个Angular JS的问题。我有两个指令。从Angular JS 1.3传递父元素到子元素的属性
angular.module('myModule', [])
.directive('myFirstDirective', function(){
return {
link: function (scope, elem, attr) {
var myAttributeToPass = attr.myFirstDirective;
scope.myAttr = myAttributeToPass;
},
controller: 'MyFirstController'
}
})
.controller('MyFirstController', function($scope){
this.returnTheParameter = function(){
return $scope.myAttr;
}
})
.directive('mySecondDirective', function(){
return {
require : ['ngModel', '^myFirstDirective'],
link : function($scope, element, attrs, ctrls) {
var ngModel = ctrls[0];
var myFirstCtrl = ctrls[1];
var theParamOfFirst = myFirstCtrl.returnTheParameter();
}
}
});
我初始化我的第一个值与一个字符串:
<div my-first-directive="foobar"> (... => my second directive is inside) </div>
我的问题是在生命周期中,返回的值始终是不确定的,因为控制器的连接之前调用。当我做一个孤立的范围的,具有:
scope: {
"myProp": "@myFirstDirective"
}
这是工作,但我不想范围隔离...
任何想法?
非常感谢!
非常感谢,它完美的作品! :) – Ndrou 2014-12-03 08:37:18