我需要两个兄弟指令来相互通信。第一个选择通过REST服务填充,当您从第一个下拉列表中选择一个值时,应该启用第二个下拉列表,并且下面的对象的子集应显示为其选项。兄弟姐妹指令之间的指令通信
例如:如果dropdown的第一个值= dog或dropdown = bird,那么第二个下拉列表应该只显示像意大利面条这样的SEries子集。但是,如果dropdown =乌龟,那么第二下拉应该显示所有的SEries。
如果无论如何要做到这一点,而不使用父母/子女的指导性沟通,这将是伟大的。
下面是对象类型:
$scope.Types = [{
"id": "dog",
"name": "dog"
}, {
"id": "cat",
"name": "cat"
}, {
"id": "bird",
"name": "bird"
}, {
"id": "turtle",
"name": "turtle"
}];
下面是一系列对象:
$scope.Series = [{
"id": "spaghetti",
"name": "spaghetti"
}, {
"id": "eggs",
"name": "eggs"
}];
})的” }];
这里是我2条指令:
.directive('selectBox', function() {
return {
restrict: 'E',
replace: true,
scope: false,
template: function (element, attrs) {
return '<div class="selectBox selector">' + '<select " class="form-control" name="' + attrs.name + '" ng-model="' + attrs.ngModel + '" ng-options="' + attrs.optexp + '"' + ' ng-disabled="' + attrs.selectdisabled + '"></select>' + '</div>';
}
};
})
.directive('selectSeries', function() {
return {
restrict: 'E',
replace: true,
scope: false,
template: function(element, attrs) {
return '<div><select class="form-control" name="' + attrs.name + '" ng-model="' + attrs.ngModel + '" ng-options="series.id as series.name for series in series" ' + ' disabled="disabled" ></select></div>';
},
link: function (scope, el, attrs) {
scope.$watch('model.PlanType', function(value,b){
if (value !== null){
//angular.element(el).children('select').removeAttr('disabled');
attrs.$set('ngOptions', 'series.id as series.name for series in series');
}
},this);
}
};
});
这里是我的fiddle
您可以尝试创建自定义发布/订阅。 [查看此答案](http://stackoverflow.com/questions/25274563/angularjs-communication-between-directives#answer-25274665) – PSL 2014-11-24 21:58:42
@PSL我听说污染$ rootcope并不是一个好主意。这个应用程序将会相当大,如果从长远来看会导致问题,我不想开始污染$ rootcope。 – gerl 2014-11-25 16:34:46
你没有污染执行事件总线的Rootcope(它不是特定于任何应用程序的业务逻辑),你基本上是扩展它,以便在内部创建一个新范围时,i,e'$ scope。$ new ()'这些方法在子范围中也是可用的并且共享相同的impl。好的污染是主观的,因为你放置了多少垃圾,特别是当你放置变量时,特定于应用程序业务逻辑的对象。另外如果你真的不想使用它,不要把它放在根作用域上使用管理pubsub的工厂。 – PSL 2014-11-25 17:00:37