Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/443.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用Angular';从JSON文件中获取特定数据;s$http.get_Javascript_Angularjs_Json - Fatal编程技术网

Javascript 如何使用Angular';从JSON文件中获取特定数据;s$http.get

Javascript 如何使用Angular';从JSON文件中获取特定数据;s$http.get,javascript,angularjs,json,Javascript,Angularjs,Json,我有一个JSON文件,其中有一个用户列表。我只想从列表中获取一个特定的用户,并使用Angular将其显示在索引页上。我找了很多,但找不到好答案。列表必须位于JSON文件中,而不是JS文件中 sampleApp.controller('userInfo', ['$scope', '$http', '$log', '$filter', function($scope, $http, $log,$filter) { $scope.results = ''; $http.defaults.heade

我有一个JSON文件,其中有一个用户列表。我只想从列表中获取一个特定的用户,并使用Angular将其显示在索引页上。我找了很多,但找不到好答案。列表必须位于JSON文件中,而不是JS文件中

sampleApp.controller('userInfo', ['$scope', '$http', '$log', '$filter',  function($scope, $http, $log,$filter) {
$scope.results = '';

$http.defaults.headers.common['X-Custom-Header'] = 'Angular.js';

$http.get('data/registered-user-list.json').
    success(function(data, status, headers, config) {
        $scope.results = data;
    })
    .error(function(data, status, headers, config) {
        // log error
    });
}]);
JSON文件:

{ "results": [
{
  "usernumber": "1",
  "userid": "manojsoni",
  "userpassword": "password",
  "useremail": "manojsoni.online@gmail.com",
  "timestamp": "1:30pm",
  "datestampe": "25/01/2016"
},
{
  "usernumber": "2",
  "userid": "jasmeet",
  "userpassword": "password",
  "useremail": "jasmeet@nagarro.com",
  "timestamp": "1:30pm",
  "datestampe": "25/01/2016"
},
{
  "usernumber": "3",
  "userid": "manoj30dec",
  "userpassword": "password",
  "useremail": "manoj.kumar@nagarro.com",
  "timestamp": "1:30pm",
  "datestampe": "25/01/2016"
},
{
  "usernumber": "4",
  "userid": "lavish",
  "userpassword": "password",
  "useremail": "lavish.sharma@nagarro.com",
  "timestamp": "1:30pm",
  "datestampe": "25/01/2016"
}    
]}
我在这里回答了类似的问题


最后,循环搜索结果并获得所需的数据/用户。

您不知道要过滤用户的创建区域


警告!
$http
成功
错误
被折旧,请使用
然后

弃用通知书

$http legacy promise方法的成功和错误已被删除 不赞成。改用标准then方法。如果 $httpProvider.useLegacyPromiseExtensions设置为false,则这些 方法将抛出$http/legacy错误

你可以这样用

$http.get('data.json').then(function(response) {
    console.log(response);
    $scope.results = response;
},
function(error) {
    $scope.results = [];
    console.log(error);
});

不推荐使用success和error方法

你可以用“then”来代替

$http.get('data/registered-user-list.json').then(function(response) {
    if (response.status == 200) {
        $scope.results = data;
    } else {
        // log error
    }
});

请把这个看一遍。读取文件。迭代结果并获得特定的用户。检查这个,我在这里回答了你想要特定用户的标准?警告!$http的成功和错误将被折旧,请改用then!见官方博士汉克斯!!我知道这两种方法,但从未注意到成功和错误都被否定了。你的答案有些错误。您可以将两个回调函数传递给then。第一个是成功,第二个是错误。看我的答案贴在1小时前,事实上它并没有错。当响应可用时调用该方法。如果响应状态为错误,则仍会调用。请参阅官方文档。文档中的内容与我告诉您的完全一致。在任何情况下都会调用该方法。你可以试试看…所以试试这个:“firstcallback”永远不会显示,因为$http失败了。。。。“文件上说的正是我告诉你的。”。请你过去一下好吗?我找不到。这很清楚<代码>$http.get('/someUrl',config.),然后(successCallback,errorCallback)两个回调函数,第一个用于成功,第二个用于错误
$http.get('data.json').then(function(response) {
    console.log(response);
    $scope.results = response;
},
function(error) {
    $scope.results = [];
    console.log(error);
});
$http.get('data/registered-user-list.json').then(function(response) {
    if (response.status == 200) {
        $scope.results = data;
    } else {
        // log error
    }
});