I'm looking for advice. My following code doesn't work, most likely because of the directive or the filter that don't get updated when the $scope.clients object changes:
$scope.clients = {
"IDa": {"position": {index: 1}, "name": "a"},
"IDb": {"position": {index: 2}, "name": "b"},
"IDc": {"position": {index: 3}, "name": 'c'},
"IDd": {"position": {index: 4}, "name": "d"},
"IDe": {"position": {index: 5}, "name": "e"},
"IDf": {"position": {index: 6}, "name": "f"},
"IDg": {"position": {index: 7}, "name": "g"},
"IDh": {"position": {index: 8}, "name": "h"},
"IDi": {"position": {index: 9}, "name": "i"}
};
I need to display 7 items (divs) based on the position property. The tricky thing is that if there are less than 7 items in the object, the view must still have 7 items just instead of the name property it'll say 'Waiting for client'. The view must display those 7 items in the order of the position 1..7.
The HTML:
<div class="streams__unit" stream client="clients | position:1">
{{ client.name || 'Waiting for client' }}
</div>
<div class="streams__unit" stream client="clients | position:2">
{{ client.name || 'Waiting for client' }}
</div>
<div class="streams__unit" stream client="clients | position:3">
{{ client.name || 'Waiting for client' }}
</div>
<div class="streams__unit" stream client="clients | position:4">
{{ client.name || 'Waiting for client' }}
</div>
<div class="streams__unit" stream client="clients | position:5">
{{ client.name || 'Waiting for client' }}
</div>
<div class="streams__unit" stream client="clients | position:6">
{{ client.name || 'Waiting for client' }}
</div>
<div class="streams__unit" stream client="clients | position:7">
{{ client.name || 'Waiting for client' }}
</div>
Here's the position filter:
angular.module('app').filter('position', function () {
return function (clients, index) {
var client = {};
for(var i in clients) {
if(clients[i].position.index === index) {
client = value;
break;
}
};
return client;
}
});
But it doesn't work since the clients object is updated later than the filter fires (i think). Here's the directive:
angular.module('app').directive('stream', function () {
return {
restrict: 'EA',
controller: function($scope) {
$scope.$watch('client', function(val) {
console.log('watch worked', val);
});
},
scope: {},
link: function (scope, element, attrs, fn) {
scope.$watch(attrs.client, function(client) {
scope.client = client;
});
}
};
});