0

Say I have angularjs modules called core, customer, and admin. Both customer and admin include core. I want both modules' sites to use the same header, which has a controller (HeaderController). When a user logs in to the site backed by the customer module, I create a constant with the user information on the core module. Wherever I go within the customer module, that constant has the data I put there. There's a link to go to the site backed the admin module and I want to be able to access that constant with the user info from admin (on the HeaderController). Below you'll find snippets of my code. Right now everything on customer works but when I go to admin I get the following:

Error: [$injector:unpr] Unknown provider: currentUserProvider <- currentUser <- HeaderController

Index.cshtml for customer

@{
ViewBag.Title = "Customer";
Layout = "~/Areas/Institutional/Views/Customer/_SiteLayout.cshtml";
}
<br />
<div>
    <ng-view></ng-view>
</div>


@section Scripts {
<script type="text/javascript">
    angular.module('core').constant('currentUser', @Html.Raw(Json.Encode(Model.Data)));
</script>
}

Module for admin

(function () {
'use strict';

angular
    .module('admin',
        ['core']);

})();

Module for customer

(function () {
'use strict';

angular
    .module('customer',
        ['core']);

})();

Controller where I need to access currentUser (constant)

(function() {
    angular
        .module('core')
        .controller('HeaderController', ['$scope', '$rootScope','currentUser',
        function ($scope, $rootScope, currentUser) {
            $scope.currentUser = currentUser;  
        }
    ]);
})();
4

3 回答 3

1

Each site has its own core module, and you have only defined the constant in the customer site page, not on the admin site page, so the constant is not available there.

Even if it was, every time you load an HTML page, everything stored in memory by the previous page goes away. So there's no way for code in one site to share in-memory constants with another site. Even if you stay on the customer site but refresh the page, you'll lose the previous value of the constant: the angular application will start from scratch.

于 2015-03-13T20:22:39.267 回答
1

除了@JB-Nizet 的回答之外,您还必须以不同的方式保存数据,例如本地存储、会话存储或 cookie。最适合您的方案

于 2015-03-13T20:52:32.560 回答
1

据我所知,您似乎正在将 SPA 嵌入到 .NET 项目中。如果您使用带角度的 asp.net mvc 路由,那么每当框架加载视图时,您的模板就会重新加载,我们的 ng-app 也会重新加载,因此您会丢失应用程序上的所有数据,就像使用 JBNizet 刷新页面一样提及。

我能想到的解决方案是使用诸如 localStorage 或缓存服务之类的东西来将您需要的数据保存在“内存”中。

于 2015-05-16T02:42:53.717 回答