Reputation: 250
I want to count inventories, by location name. All inventories have there location place. I did something like this but it doesnt work.
$scope.stored = function(name) {
var count = 0;
for(var i = 0; i < $scope.inventories.length; i++){
if($scope.inventories[i].location.name == name){
count += 1;
}
}
return count;
};
And in template i put inside location name, currentLocation.name returns selected location name, so passed value is correct.
<a ng-click="stored(currentLocation.name)">{{location.name}}</a>
<pre>{{stored}}</pre>
My json api where i get all data
{
meta: {
limit: 20,
next: null,
offset: 0,
previous: null,
total_count: 3
},
objects: [
{
assigned: {},
barcode: "23423432423423",
count: 1,
cover: "/media/static/images/tv_3.jpg",
created: "2014-03-17T10:06:32.309661",
id: 1,
location: {
administrator: {},
city: "London",
country: "Afrika",
id: 1,
name: "Eriks noliktava",
postalCode: "1046",
resource_uri: "/api/v1/location/1",
street: "Zasulauka iela 11"
},
manufacturer: "LG",
model: "M3000",
resource_uri: "/api/v1/inventory/1",
tags: {
id: 1,
resource_uri: "/api/v1/tags/1",
tags: "TV"
}
},
{
assigned: {},
barcode: "888",
count: 1,
cover: "/media/static/images/no-image.png",
created: "2014-03-17T12:14:58.833429",
id: 2,
location: {
administrator: {},
city: "Riga",
country: "Latvia",
id: 2,
name: "Martina noliktava",
postalCode: "1050",
resource_uri: "/api/v1/location/2",
street: "Rigas iela 27"
},
manufacturer: "Iphone",
model: "5S",
resource_uri: "/api/v1/inventory/2",
tags: {
id: 2,
resource_uri: "/api/v1/tags/2",
tags: "Phone"
}
},
Upvotes: 2
Views: 133
Reputation: 250
I changed my code so it is working, if somebody needs.
$scope.stored = function(name) {
$scope.count = 0;
for(var i = 0; i < $scope.inventories.length; i++){
if($scope.inventories[i].location.name == name){
$scope.count = $scope.count + 1;
}
}
return $scope.count;
};
And in template
{{stored(currentLocation.name)}}
Upvotes: 1
Reputation: 92735
You have counted and return values to ng-click
event. Instead you can store it inside scope.
var count = 0;
// Calculate count
// Assign the count to $scope
$scope.count = count;
Then use count
anywhere in your template.
Upvotes: 0