I need two sibling directives to communicate with each other. The first select is populated through REST service and when you select a value (from the first dropdown), the second dropdown should be enabled and a subset of the object below should be displayed as its options.
For ex: if the first value of the dropdown = dog or dropdown = bird, then the 2nd dropdown should only display a subset of SEries like spaghetti. But if dropdown = turtle then 2nd dropdown should display all of SEries.
If theres anyway to do this without using parent/children directive communication that would be great.
Here are the Types object:
$scope.Types = [{
"id": "dog",
"name": "dog"
}, {
"id": "cat",
"name": "cat"
}, {
"id": "bird",
"name": "bird"
}, {
"id": "turtle",
"name": "turtle"
}];
Here are the SEries object:
$scope.Series = [{
"id": "spaghetti",
"name": "spaghetti"
}, {
"id": "eggs",
"name": "eggs"
}];
})s" }];
Here are my 2 directives:
.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);
}
};
});
Here is my fiddle