One dimensional array of strings being parsed to 2d by angular resource

前端 未结 2 945
时光说笑
时光说笑 2020-11-28 23:59

The following JSON response from the server

[
    \"hello\",
    \"world\"
]

is being parsed into a 2d array by this ngResource service

2条回答
  •  盖世英雄少女心
    2020-11-29 00:35

    I have been struggling with this as well. Here is my solution by adjusting the service slighly by using query

    var app = angular.module('testApp', ['ngResource']);
    
    app.factory('Name', function($resource, $sce) {
      var path = "test.json";
    
      return $resource(path, {}, {
        query: {
          method: 'GET',
          isArray: false
        }
      })
    });
    
    app.controller('testController', function($scope, Name) {
      $scope.result;
    
      $scope.getResult = function() {
        Name.query(function(data) {
          $scope.result = data;
        });
      };
    
      $scope.getResult();
    });
    

    HTML:

    
    
    
    
    
      
      
      
    
      
    
    
    
      

    {{result.surname}}

    and the JSON file:

    {
        "name": "Homer",
        "surname":  "Simpson",
        "Town": "Springfield"
    }
    

    and also working Plunker if interested: http://plnkr.co/edit/SwqlZyqZ4zfcpaLxaf39

    Hope this help someone ...

提交回复
热议问题