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

时间:2021-08-30 05:11:15

i need one help.I need to fetch data from my data.json file using the unique id.I am explaining my code below.

我需要一个帮助。我需要使用唯一的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) {

     });

     }

Here i need when the condition (id==2) will match in json file it will fetch those specific data.Please help me.

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

2 个解决方案

#1


2  

Try it:

尝试一下:

//...
}).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;
        }
    }
}).
//...

#2


0  

You can make the Ajax call and then evaluate the response. For example:

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

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

#1


2  

Try it:

尝试一下:

//...
}).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;
        }
    }
}).
//...

#2


0  

You can make the Ajax call and then evaluate the response. For example:

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

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