AngularJS,Iam从服务器获取对象数组,我无法在UI中获取它

时间:2018-02-14 14:13:49

标签: javascript angularjs json

我从服务器获取对象数组作为Json,当我试图从我的控制台点击我的服务URI时,我的控制台下面的数组。" angular.js:13920

row,col = map(int,input().split())
matrix = list()

for i in range(row):
    r = list(map(int,input().split()))
    matrix.append(r)

trans = [[0 for y in range(row)]for x in range(col)]

for i in range(len(matrix[0])):
    for j in range(len(matrix)):
        trans[i][j] = matrix[j][i]     

for i in range(len(trans)):
    for j in range(len(trans[0])):
        print(trans[i][j],end=' ')
    print(' ')

register.html

Error: [$resource:badcfg] http://errors.angularjs.org/1.5.8/$resource/badcfg?p0=get&p1=object&p2=array&p3=GET&p4=%2Fagtools%2FfetchStates"

StateController.JS

  <select class="form-control topMarginForRegister"  ng-
     model="LoginDetails.state" ng-click="getStatesData();">
     <option>Select state</option>
     <option ng-repeat="" value="{{state}}">{{state}}</option></select>

LoginFactory.JS

 (function() {
        function loginModalController($scope, LOGIN_CONSTANTS, loginFactory) {
          $scope.getStatesData = function() {
            loginFactory.getStatesData($scope.state).then(function(response) {
                console.log("success");
              })
              .catch(function(error) {
                console.log("error");
              });
          };
        })();

JSON我从FROM SERVER:

 (function() {
    function loginFactory($q, LOGIN_CONSTANTS, $resource) {
      function getStatesData(state) {
        var stateData = $resource(LOGIN_CONSTANTS.FETCH_STATES_URL); //my service URL
        var defered = $q.defer();
        stateData.get(
          function(response) {
            defered.resolve(response);
          },
          function(error) {
            defered.reject(error);
          });
        return defered.promise;
      };
    })();

有人请帮助我如何清除该错误,以便我从server.i尝试$ Resource.query仍然无法获取数据

2 个答案:

答案 0 :(得分:0)

如错误地写,您正在尝试解析Array而不是Object。 理想情况下,这应该已经使用$resource.query进行了修复,但您尝试了它并且无效。

然后我的建议是将您的响应从数组更改为对象。 我不确定你的情况是否可行。

{
  "countryData": [
    {
      "city": [
        "ABBE",
        "ADAM"
      ],
      "state": "ALBAMA",
      "country": "US"
    },
    {
      "city": [
        "ABDE",
        "JODA"
      ],
      "state": "ALAKA",
      "country": "US"
    }
  ]
}

答案 1 :(得分:0)

工作演示

var myApp = angular.module('myApp',[]);

myApp.controller('MyCtrl', function($scope) {
    $scope.data = [
    {
     "city":["ABBE","ADAM"],
     "state":"ALBAMA",
     "country":"US"
    },

    {
     "city":["ABDE","JODA"],
     "state":"ALAKA",
     "country":"US"
    }
    ];
});
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
</head>
<body ng-app="myApp">
<div ng-controller="MyCtrl">
  <select name="state" ng-model="state">
      <option>Select state</option>
     <option ng-repeat="details in data" value="{{details.state}}">{{details.state}}</option></select>
</div>
</body>
</html>