如何使用Angular.js从json文件中使用id获取特定数据

时间:2015-12-31 15:59:39

标签: javascript angularjs json

我需要一个帮助。我需要使用唯一的id从我的data.json文件中获取数据。我正在解释下面的代码。

 $scope.editProfileData=function(){
         var editid={'id':2};
         $http({
         method: 'POST',
         url: 'profile.json',
         data:editid
     }).then(function successCallback(response){

     },function errorCallback(response) {

     });

     }

这里我需要条件(id==2)在json文件中匹配时它将获取那些特定数据。请帮助我。

2 个答案:

答案 0 :(得分:1)

试一试:

//...
}).then(function successCallback(response){
    var i, l = response.data.length;
    for(i = 0; i < l; i++){
        if( response.data[i].id == 2){
            fetch( response.data[i]); // your fetch code here
            break;
        }
    }
}).
//...

答案 1 :(得分:0)

您可以进行Ajax调用,然后评估响应。例如:

[...]
   .then(function successCallback(response){
        if (response.id == "2") {
            doSomeStuff();
        }
[...]