I have a JSON example with multiple records, assuming that each name is unique. I am looking to filter out an object by name in the controller to avoid constant iteration through all the records using ng-repeat in my HTML.
{
"records": [
{
"Name" : "Alfreds Futterkiste",
"City" : "Berlin",
"Country" : "Germany"
},
{
"Name" : "Berglunds snabbköp",
"City" : "Luleå",
"Country" : "Sweden"
},
{
"Name" : "Centro comercial Moctezuma",
"City" : "México D.F.",
"Country" : "Mexico"
}
]
}
Ideally, I want to use the filtered object within my HTML like this:
<p> {{ filteredObj.Name}} </p>
In my controller, I am having issues applying the filter correctly and can't figure out why.
var myApp = angular.module('myApp', []);
myApp.controller('myCtrl', ['$scope', '$http', function($scope, $http) {
$http.get('customers.json').success(function (data) {
//I am struggling with the filter method here
$scope.filteredObj = $filter('filter')(data.records, {name: "Alfreds Futterkiste");
});
}]);
How should I correctly utilize the $filter function in the controller?