Angular Factory变量不会更新-实例获取旧值 [英] Angular Factory Variables Won't Update - Instance gets old values

查看:48
本文介绍了Angular Factory变量不会更新-实例获取旧值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个资源工厂,该工厂建立用于访问我们API的对象.我使用环境变量来确定URL的基本部分-当管理员用户参与"客户帐户时是否包括帐户/id"路径段.

I have a resource factory that builds objects for accessing our API. I use an environment variable to determine the base part of the URL - whether or not to include 'account/id' path segments when the admin user is 'engaging' a client account.

保存'engagedAsId'的sessionStorage项不会被读取,尽管对于使用帐户后创建的实例而言.需要完全重新加载应用程序才能获取该更改.这是工厂代码:

The sessionStorage item that holds the 'engagedAsId' doesn't get read, though for instances created after engaging an account. It requires a full reload of the app to pick up that change. Here is the factory code:

myapp.factory('ResourceSvcFactory',
    ['$rootScope', '$resource', 
    function ($rootScope, $resource) {

    function ResourceSvcFactory (endpoint) {
        // vvv problem is here vvv
        var accountId = sessionStorage.getItem('engagedAsId');
        var apiPath = (accountId != null) 
            ? '/api/account/' + accountId + endpoint 
            : '/api' + endpoint;

        var Resource = $resource(apiPath+':id/',{
            // default params
            id:''
        },{
            // custom actions 
            update: {method: 'PUT'}
        });

        return Resource;
    }

    return ResourceSvcFactory;
}]);

myapp.factory('AssetsResource', ['ResourceSvcFactory', function (ResourceSvcFactory) {

    var endpoint = '/assets/';
    var Resource = ResourceSvcFactory(endpoint);

    return Resource;
}]);

我这样在我的控制器中实现了这一点:

I implement this in my Controller like this:

myapp.controller('AssetGroupListCtrl', [ 'AssetgroupsResource', function (AssetgroupsResource) {

    var AssetGroups = AssetgroupsResource;

    // ... rest of controller
}]);

当我运行它时,它工作正常.但是,如果我在没有完全重载的情况下更改了sessionStorage中的参与状态,则控制器中的实例将不会选择新路径.

When i run this it works fine. But, if i change the engaged status in the sessionStorage without a full reload, the instance in the controller does not pick up the new path.

是否有一种刷新"实例的方法?...自动吗?

Is there a way to 'refresh' the instance? ...automatically?

推荐答案

经过数小时的研究,看来我要在这个问题中尝试做的事情的根本缺陷是:我正在尝试使用单身人士"作为班级".来自文档:

After hours of research, it appears that the fundamental flaw in what I'm trying to do in the question is this: I'm trying to use a 'singleton' as a 'class'. from the docs:

注意:Angular中的所有服务都是单例.这意味着注入器最多使用每个配方一次来创建对象.然后,注入器将参考保存为将来的所有需求. http://docs.angularjs.org/guide/providers

我的解决方法是在返回对象的方法内部创建$ resource.这是一个例子:MyApp.factory('AssetgroupsResource',['$ rootScope','$ resource',函数($ rootScope,$ resource){

My work around was to create the $resource inside a method of a returned object. Here is an example: MyApp.factory('AssetgroupsResource', ['$rootScope', '$resource', function ($rootScope, $resource) {

    return {
        init: function () {
            var accountId = sessionStorage.getItem('engagedAsId');
            var apiPath = (accountId != null) 
                ? '/api/account/' + accountId + endpoint 
                : '/api' + endpoint;
                // default params
                id:''
            },{
                // custom actions 
            });
            return Resource;
        }
    }
}]);

这使得可以在适当的时候在控制器中构建对象:

This made it possible to build the object at the right time in the controller:

MyApp.controller('AssetGroupListCtrl', ['Assetgroups', function (Assetgroups) {
    var Assetgroups = AssetgroupsResource.init();
    // now I can use angular's $resource interface
}]);

希望这对某人有帮助.(或者您会告诉我如何用3行完成所有操作!)

Hope this helps someone. (or you'll tell me how this all could've been done in 3 lines!)

这篇关于Angular Factory变量不会更新-实例获取旧值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆