【问题标题】:Fetching particular json key from controller AngularJS 1从控制器AngularJS 1获取特定的json键
【发布时间】:2018-04-09 10:10:22
【问题描述】:

您好,我是 AngularJS 的新手,正在尝试分别获取 json 密钥数据并将其显示到控制台窗口。我可以获取整个 json 数据,但无法获取特定节点内的数据。我哪里错了?

Service.js

app.service("AppService", function($http) {
    return {
       network: [],
       getAllService: function(network){

        return $http({
            method: 'GET',
            url: 'http://99.126.4.6:3200/app/json/allDatas',
            headers: {'Content-Type': 'application/json'} 

        })
                .then(function(data) {
                    return data;
                })
    }
    }
});

控制器:-

app.controller('getController', ['$scope','$http','AppService','$localStorage', function ($scope,$http,AppService,$localStorage) {


            $scope.load = AppService.getAllService();
            $scope.load.then(function(data) {
            $scope.getAllData = data; 
            $scope.getId = data.ID;
            $scope.getName = data.Name;
            $scope.getDescription = data.Description;
            console.log($scope.getId + $scope.getName + $scope.getDescription);
            })

}]);

当我控制台 getAllData 时,我可以看到整个 json 响应。但无法获取内部键。

JSON 响应:-

Data
Array(1)
0
:
{$id: "1", ID: 1, Name: "APP", Description: "Something", Segments: Array(3)}

【问题讨论】:

    标签: angularjs json api get


    【解决方案1】:

    您将旧语法与新语法混合在一起:.success vs. .then

    .then() 返回一个Http Promise,它将您的响应包装在一个对象中。要提取数据,您需要先访问.data

    从以下位置修复此行:

    .then(function(data) {
      return data;
    })
    

    .then(function(data) {
      return data.data;
    })
    

    【讨论】:

    • 现在我在控制台中收到此响应。 (22) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}] {$id: "1", ID: 1, Name: "App", Description: "Some description", Segments: Array(3)} 。数据也存储在我的控制器上$scope.getId = data.ID; $scope.getName = data.Name; $scope.getDescription = data.Description;
    • @WhoAmI 我想你也是store it in an array出于任何原因),试试data.data[0];作为解决方法
    • 感谢您的帮助。我正在获取信息。
    • 嘿!该数组包含许多对象,我想显示整个对象。 [0] 将只显示第一个对象。
    • @WhoAmI 然后使用data.data 并更改您的其他逻辑以改为接受数组。比如$scope.getAllData可以用在ng-repeat="data in getAlldata"里面,然后你可以用{{data.ID}}等拉出属性(此时已经超出了你的问题范围,自己解决,或者问一个新的)
    【解决方案2】:

    数据是一个数组,所以通过索引访问它的值

        $scope.load = AppService.getAllService();
        $scope.load.then(function(data) {
        angular.forEach(data, function(value) {
           console.log(value.ID+"   "+value.name++"   "+value.escription);
        });
        })
    

    【讨论】:

    • 好的,这也有效。谢谢你。我想显示数组中包含的每个对象。
    • 循环数据并控制它 angular.forEach(data, function(value) { console.log(value.ID+" "+value.name++" "+value.escription); });
    • 抛出错误:Uncaught SyntaxError: missing ) after argument list[ng:areq] http://errors.angularjs.org/1.3.15/ng/areq?p0=getController&p1=not%20aNaNunction%2C%20got%20undefined
    • 已编辑,请立即查看
    • 还是不正确。虽然我做了更正。 $scope.load = AppService.getAllService(); $scope.load.then(function(data) { angular.forEach(data, function(value) { $scope.getAllData = data; console.log(value.ID +value.Name + value.Description); }); }); 对我来说很好用。感谢您的帮助。
    猜你喜欢
    • 2019-12-14
    • 1970-01-01
    • 1970-01-01
    • 2015-09-02
    • 1970-01-01
    • 2016-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多