Amir Ur Rehman
Amir Ur Rehman

Reputation: 670

AngularJs params give all objects with one id

I want to get a single object from json file using AngularJs params, but it give me all objects.

This is the code I've used,

 (function () {

    "user strict";

    angular
        .module("myApp")
        .controller("indexCtrl", function ($scope, $http, $routeParams) {

        var workId = $routeParams.id;

        $http({
            url: "data/work.json", 
            method: "GET",
            params: {id: workId}
        }).then(function(sitedata) {
            $scope.workDetail = sitedata.data;
        });  

    });
})();

Please help me. Thanks

Upvotes: 1

Views: 30

Answers (1)

devqon
devqon

Reputation: 13997

If you're loading a json file it will just return the whole content, you'll have to filter in the then callback:

$http({
    url: "data/work.json", 
    method: "GET"
}).then(function(sitedata) {
    var match = sitedata.data.filter(function(item) {
        return item.id == workId;
    });
    if (match.length) {
        $scope.workDetail = match[0];
    }
});  

Upvotes: 1

Related Questions