I'm new to AngularJS and need some help. I have two directives (parent and child). When I click a link, the parent directive will fire a broadcast event that the child directive is listening for. Mainly I can't get the broadcast listener to capture the broadcasted event when it is fired. Here is my code so far:
<div all-checkboxes-broadcast>
<a href="" ng-click="checkAll()">Select All</a>
<a href="" ng-click="clearAll()">Clear All</a>
<div all-checkboxes-listeners>
<input type="checkbox" />
</div>
</div>
AllCheckboxesBroadcast.js
"use strict";
app.directive('allCheckboxesBroadcast', function () {
return {
restrict: 'A',
controller: ['$scope',
function($scope) {
//select all checkboxes
$scope.checkAll = function () {
$scope.$broadcast('allCheckboxes',true);
};
//de-select all checkboxes
$scope.clearAll = function () {
$scope.$broadcast('allCheckboxes',false);
};
}
]
}
})
AllCheckboxesListener.js
"use strict";
app.directive('allCheckboxesListener', function () {
return {
restrict: 'A',
require: '^allCheckboxesBroadcast',
link: function() {
if ($scope.$on('allCheckboxes') == true) {
angular.forEach(element.find('input[type=checkbox]'), function(){
this.prop('checked',true);
});
}
}
}
})
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…