Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs Angular.js-处理在多个位置使用的数据的正确方法_Angularjs_Angularjs Routing - Fatal编程技术网

Angularjs Angular.js-处理在多个位置使用的数据的正确方法

Angularjs Angular.js-处理在多个位置使用的数据的正确方法,angularjs,angularjs-routing,Angularjs,Angularjs Routing,我正在尝试在Angular.js中设置一个相对简单的在线购物应用程序 我使用ng route模块显示项目列表视图和项目详细视图。加载项目列表路由时,我使用resolve加载一个json文件,其中包含项目列表描述、图像url、价格等 app.config(['$routeProvider', function($routeProvider) { $routeProvider. when('/items', { templateUrl: 'partia

我正在尝试在Angular.js中设置一个相对简单的在线购物应用程序

我使用ng route模块显示项目列表视图和项目详细视图。加载项目列表路由时,我使用resolve加载一个json文件,其中包含项目列表描述、图像url、价格等

app.config(['$routeProvider', function($routeProvider) {
    $routeProvider.
        when('/items', {
            templateUrl: 'partials/item-list.html',
            controller: 'ItemListController',
            resolve: {
                items: ["Item", function(Item) {
                    return Item.fetch();
                }]
            }
        }).
        when('/items/:itemID', {
            templateUrl: 'partials/item-detail.html',
            controller: 'ItemDetailController',
            resolve: {
                dish: ["$route", "Item", function($route, Item) {
                    return Item.fetch($route.current.params.itemID);
                }]
            }
        }).
        otherwise({
            redirectTo: '/items'
        });
}]);
我还创建了购物车服务来处理向购物车添加/删除项目

services.factory("Cart", function() {
    return {
        add: function(item) {
            ...
        },
        remove: function(item) {
            ...
        }
        items: ...
    }
});

此购物车已加载/保存到cookie。这是最佳做法吗?。只有项目的itemID保存到cookie中。当购物车从Cookie中重新加载时,我想再次从服务器中获取物品,以防描述或价格发生变化。在这里执行另一个http查询来加载项目似乎是不够的,因为在加载项目列表路由时,路由器已经执行了一个请求。这方面的最佳实践是什么?

一个快速成功的解决方案是创建一个包含购物车的服务

services.factory("Cart", function() {
    return {
        add: function(item) {
            ...
        },
        remove: function(item) {
            ...
        }
        items: ...
    }
});
此服务可以使用angular的“$cache/$cacheFactory”将其保存在内存中。顺便说一句,我认为最好使用内存存储小数据,使用会话存储存储大数据,使用本地存储存储会话之间要保留的数据。这比饼干好多了

正如你在Wishtack.com上看到的,根本没有饼干

顺便说一句,在Wishtack.com上,我们使用了一个自定义模块,我们将很快在github.com上发布。在顶部重新启动时,您应该使用它,而不是处理缓存等的$http。。。并使用对象,以便我们可以进行如下调用:

CartProduct.all().getListLazy();
因此,延迟调用将尝试从缓存中获取值,而不是每次都调用服务器

同时我们发布模块,您可以简单地使用如下服务:

(function () {

/* We use dejavu for classes as we hate functions at Wishtack :). */
var User = dejavu.Class.declare({

    _productList: null,

    initialize: function initialize(args) {
        ... load cart using "args.userId" ...
    },

    cartProductList: function cartProductList() {
        ... or load it here lazily ...

        return this._productList;
    }

});

yourModule.factory('User', function () {

    return User;

});

})();
这样使用它

function (User) {
    new User({userId: userId}).cartProductList();
}

为什么会有另一个http查询。Item.fetch第一次不做作业吗?为什么需要再次获取它们?从cookieWell加载当前购物车后,如果您在获取物品列表时加载与物品相关的所有信息,然后您希望从服务器重新获取信息以确保其最新,则将有第二次获取来加载物品详细信息,除了这样做,真的没有其他简单的选择。通常的做法是显示数据列表,然后为该数据的特定记录获取更多数据。