繁体   English   中英

我如何使用angularjs($ http.get)根据在JSON文件中输入的特定属性解析json数据?

[英]How would i parse the json data according to the particular property entered in JSON file using angularjs($http.get)?

我有如下的JSON文件

{
  "colorsArray":[{
    "colorName":"red",
    "hexValue":"#f00"
  },
  {
    "colorName":"green",
    "hexValue":"#0f0"
  },
  {
    "colorName":"blue",
    "hexValue":"#00f"
  },
  {
    "colorName":"cyan",
    "hexValue":"#0ff"
  },
  {
    "colorName":"magenta",
    "hexValue":"#f0f"
  },
  {
    "colorName":"yellow",
    "hexValue":"#ff0"
  },
  {
    "colorName":"black",
    "hexValue":"#000"
  }
]}

以及我的js文件,具体如下:.....我正在使用$http.get功能读取json文件

var app = angular.module('app', ['ngTagsInput']);
app.controller('MainCtrl', function($scope, $http) {

  $scope.loadTags = function(query) {
      return $http.get('tags.json');
  };
});

当使用AngularJS在$http.get外部引用JSON文件时,如何仅将colorName作为解析元素?

对json文件发出请求时,您不能即时查询json file 一种方法和最有效的方法是将带有查询的HTTP调用作为参数发送到后端服务方法,并从服务器端执行并返回结果。

另一种方法是从客户端过滤结果。 这样,所有json数据都将作为响应返回,然后需要从控制器中过滤相关数据'

$scope.loadTags = function(query) {
    return $http.get('tags.jon').then(function(response) {
        var data = response.data.map(function(obj) {
            return obj.colorName;
        })
        return data;
    })
};

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM