angularjs 在他兄弟之前加载一个兄弟指令
angularjs load one sibling directive before his brother
我在同一层次结构中有两个指令:指令 1 和指令 2
我希望 directive2 在 directive1 之前执行他的控制器而不更改 html directives hierarchy
app.directive('directive1', [function() {
return {
restrict: 'E',
scope: {
},
templateUrl: 'My/views/directive1.html',
controller: ['$scope', function ($scope) {
console.log("Controller of Directive 1");
}]
}
}]
)
app.directive('directive2', [function() {
return {
restrict: 'E',
templateUrl: 'My/views/directive2.html',
controller: ['$scope','$timeout', function ($scope,$timeout) {
console.log("Controler of Directive 2");
}]
}
}]
);
<div ng-controller="test" >
<directive1></directive1>
<directive2></directive2>
</div>
我的第一个问题是为什么??,但理论上您可以添加一个超时,这会将指令推送到堆栈的末尾。等待来自您正在寻找的指令的广播事件...有点:
app.directive("firstDir", function(){
return {
restrict : 'E',
controller : function($scope){
this.data = 'init value';
this.set = function(value){
//EMIT THE EVENT WITH DATA
$scope.$emit('FIRST_DIR_UPDATED', value);
this.data = value;
// communication with second Directive ???
}
},
controllerAs : 'firstCtrl'
};
});
app.directive("secondDir", function(){
return {
restrict : 'E',
controller : function($scope){
var _that = this;
//LISTEN TO THE EVENT
$scope.$on('FIRST_DIR_UPDATED', function(e, data){
_that.data = data;
});
this.data = 'init value';
},
controllerAs : 'secondCtrl'
};
});
这是来自 answer
的 POC
所以想法是一个指令发出或广播一条消息,而另一个捕获是..您还可以添加一个标志,这样这将在条件下发生
这是您要找的吗?
我在同一层次结构中有两个指令:指令 1 和指令 2 我希望 directive2 在 directive1 之前执行他的控制器而不更改 html directives hierarchy
app.directive('directive1', [function() {
return {
restrict: 'E',
scope: {
},
templateUrl: 'My/views/directive1.html',
controller: ['$scope', function ($scope) {
console.log("Controller of Directive 1");
}]
}
}]
)
app.directive('directive2', [function() {
return {
restrict: 'E',
templateUrl: 'My/views/directive2.html',
controller: ['$scope','$timeout', function ($scope,$timeout) {
console.log("Controler of Directive 2");
}]
}
}]
);
<div ng-controller="test" >
<directive1></directive1>
<directive2></directive2>
</div>
我的第一个问题是为什么??,但理论上您可以添加一个超时,这会将指令推送到堆栈的末尾。等待来自您正在寻找的指令的广播事件...有点:
app.directive("firstDir", function(){
return {
restrict : 'E',
controller : function($scope){
this.data = 'init value';
this.set = function(value){
//EMIT THE EVENT WITH DATA
$scope.$emit('FIRST_DIR_UPDATED', value);
this.data = value;
// communication with second Directive ???
}
},
controllerAs : 'firstCtrl'
};
});
app.directive("secondDir", function(){
return {
restrict : 'E',
controller : function($scope){
var _that = this;
//LISTEN TO THE EVENT
$scope.$on('FIRST_DIR_UPDATED', function(e, data){
_that.data = data;
});
this.data = 'init value';
},
controllerAs : 'secondCtrl'
};
});
这是来自 answer
的 POC所以想法是一个指令发出或广播一条消息,而另一个捕获是..您还可以添加一个标志,这样这将在条件下发生
这是您要找的吗?