简体   繁体   English

从AngularJS中的数组中获取对象的正确方法

[英]Proper way to get an object out of an array in AngularJS

I'm just getting started with AngularJS and wondering if there's a better way to do this: 我刚刚开始使用AngularJS,想知道是否有更好的方法可以做到这一点:

I have a factory providing me an array of objects representing products. 我有一家工厂,向我提供代表产品的一系列对象。 (This is hard-coded for now - the products will eventually be returned from a RESTful API.) (目前已进行硬编码-产品最终将通过RESTful API返回。)

app.factory('Products', function(){
    var products = [
        { slug: 'wireframe-kit', name: 'Wireframe Kit', price: 27 },
        { slug: 'ui-kit', name: 'UI Kit', price: 29 },
        { slug: 'ui-icons', name: 'UI Icons', price: 39 }
    ];
    return products;
});

Assuming a route such as /product/wireframe-kit, here's how the router is currently setup: 假设使用/ product / wireframe-kit之类的路由,以下是路由器当前的设置方式:

app.config(function($routeProvider){
    $routeProvider.
        when('/product/:slug', {
            templateUrl: 'template/product.html',
            controller: 'productController'
        });
    }
}); 

I'm then using the slug from the url to retrieve the correct product from this array in the controller: 然后,我使用url中的段从控制器中的此数组中检索正确的产品:

app.controller('productController', function($scope, $routeParams, Products){
    Products.forEach(function(product){
        if( product.slug == $routeParams.slug) {
            $scope.product = product;
        }
    });
});

My concern is with the forEach loop in the controller - I'm sure there's got to be an easier, more efficient way to parse this products array to get the object I want. 我关心的是控制器中的forEach循环-我确定必须有一种更简单,更有效的方法来解析此积数组以获取所需的对象。 Any ideas? 有任何想法吗?

You can change your api to return object (if all products have unique slug). 您可以将api更改为返回对象(如果所有产品都具有唯一的子弹)。 This way you can access products by key(slug) name: 这样,您可以通过密钥(子弹)名称访问产品:

app.factory('Products', function(){
    var products = {
        'wireframe-kit': { slug: 'wireframe-kit', name: 'Wireframe Kit', price: 27 },
        'ui-kit': { slug: 'ui-kit', name: 'UI Kit', price: 29 },
        'ui-icons': { slug: 'ui-icons', name: 'UI Icons', price: 39 }
    };
    return products;
});


app.controller('productController', function($scope, $routeParams, Products){
    $scope.product = Products[$routeParams.slug];
});

If we had a REST API set up pragmatically it would like 如果我们通过实用的方式设置了REST API,

/products - being your 'collection' /products是您的“收藏”

and /products/:id - being your 'model' /products/:id成为您的“模特”

As you are using static data, it would still be worth putting the data on the server as static files to mimic these patterns in relevant web folders. 在使用静态数据时,仍然有必要将数据作为静态文件放置在服务器上,以模仿相关Web文件夹中的这些模式。

Then with Angular you can set up the routes to do get requests for these 'endpoints' 然后使用Angular,您可以设置路由以获取对这些“端点”的请求

  • /products/ - gives you $scope.collection /products/ -给您$scope.collection
  • /products/<id of products> - gives you $scope.model /products/<id of products> -为您提供$scope.model

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

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