简体   繁体   English

哪个是使用Angular检索json数据的最佳方法

[英]Which is the best way to retrieve json data using Angular

I have data which I segregated just like the JSON structure below 我有数据,我隔离就像下面的JSON结构

{
    "Id": {
        "1": [
            {
                "Item1": "Item One",
                "Item2": "Item Two",
                "Item3": "Item Three"
            }
        ],
        "2": [
            {
                "Item1": "Item One",
                "Item2": "Item Two",
                "Item3": "Item Three"
            }
        ],
        "3": [
            {
                "Item1": "Item One",
                "Item2": "Item Two",
                "Item3": "Item Three"
            }
        ]
    }
}

But I also have the data in separate json files for each id. 但我也在每个id的单独json文件中有数据。 And the data will be constant for most of the time. 数据在大多数情况下都是恒定的。 But it is going to accessed much frequently. 但它会经常访问。

I know how to write the code to access data in both the mentioned ways. 我知道如何编写代码以上述两种方式访问​​数据。 But what I am confused is which way would be better and faster to access the data. 但令我感到困惑的是,访问数据的方式会更好,更快。

One way you could approach this would be to inject a service into your controllers which will have a function to return a promise for this data. 您可以采用的一种方法是将一个服务注入到您的控制器中,该控制器将具有返回此数据的承诺的功能。 This is advantageous because you'll be able to inject and reuse this service "app wide" and you can also cache the response for performance. 这是有利的,因为您将能够“应用程序范围”注入和重用此服务,您还可以缓存性能响应。 Here is an example implementation... 这是一个示例实现......

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

app.service('DataService', ['$http', function ($http) {
    this.getData = function(){
      return $http.get('data.json', { cache: true })
    }
}]);

app.controller('ctrl', ['$scope', 'DataService', function($scope, DataService) {
    DataService.getData().then(function(response) {
        console.log(response.data);
    });
}]);

Plunker Link - demo Plunker Link - 演示

The only thing to be mindful of if is the call to .then since this in an asynchronous call. 唯一需要注意的是在异步调用中调用.then


Another way you can do this, and this is making some assumptions here, could include the following scenario: Assume this "constant" data is something like some configuration data and you wish to get your hands on this without resolving asynchronous promises in your controllers later on. 你可以做到这一点的另一种方法,这就是在这里做一些假设,可能包括以下场景:假设这个“常量”数据类似于某些配置数据,你希望在不解决控制器中的异步承诺的情况下亲自动手上。 You could manually bootstrap your app after initially retrieving this data, offering the convenience of knowing you'll have this data up front. 您可以在最初检索此数据后手动引导您的应用程序,方便您知道您将预先获得此数据。

To do this, lets create an AngularJS constant called data 为此,我们创建一个名为dataAngularJS常量

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

(function() {
  var initInjector = angular.injector(['ng']);
  var $http = initInjector.get('$http');
  $http.get('data.json').then(
    function (response) {

      app.constant('data', response.data);

      angular.element(document).ready(function() {
          angular.bootstrap(document, ['app']);
      });
    }
  );
}());

app.controller('ctrl', ['$scope', 'data', function($scope, data) {
    console.log(data)
}]);

You can see this is simplified, at least at our controller level. 您可以看到这是简化的,至少在我们的控制器级别。 We just inject data as a dependency which already has resolved our $http.get() for us. 我们只是将data作为依赖注入, 已经为我们解析了$http.get()

Plunker Link - demo - constants Plunker Link - 演示 - 常量

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

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