I have a directive called meetingList
with its own isolated scope that receives a list of meetings and displays the filtered items based on some criteria. The controller for this directive holds two variables the meetingList
and the filteredMeetingList
. The scope where this directive is created needs to know how many items the directive is displaying (filteredMeetingList.length
) in order to do something (outside the directive). I am currently exposing this variable using two way databinding on the isolated scope to send the variable outside.
Is there a better way of doing this? is there some kind of standard for this?
I set up a simple Plunker to illustrate the scenario.
HTML
<body ng-app="test">
<div ng-controller="AppCtrl as appCtrl">
<meeting-list meeting-list="appCtrl.meetingList" filtered-meetings-count="appCtr.filteredMeetingsCount"></meeting-list>
Total meetings {{appCtrl.meetingList.length}} <br>
Meetings being displayed by the meetingList directive {{appCtr.filteredMeetingsCount}}
</div>
</body>
Javascript
var app = angular.module('test', []);
app.directive('meetingList', function(){
return{
restrict: 'E',
scope: {
meetingList: '=',
//this is the variable that I use to expose the length of the filtered meetings to the external scope
filteredMeetingsCount: '='
},
controller: function($scope){
var self = this;
var meetingList = $scope.meetingList;
var filteredMeetingList = $scope.filteredMeetingList = [];
filterMeetings();
//Here I set the value after filtering
$scope.filteredMeetingsCount = filteredMeetingList.length;
function filterMeetings()
{
filteredMeetingList.push(meetingList[1]);
filteredMeetingList.push(meetingList[2]);
}
},
template: '<div><span ng-repeat="meeting in filteredMeetingList"> {{meeting}} </span></div>'
}
});
app.controller('AppCtrl', function($scope){
var self = this;
self.meetingList = ["meeting1", "meeting2", "meeting3"];
});