【发布时间】:2017-01-23 01:41:10
【问题描述】:
我在一个控制器中有数据,现在我想与另一个控制器共享它,但两个控制器都有不同的模块。我用过 $rootscope 但它没有用。我已经使用了服务它也没有工作。链接在这里Service
还有其他办法吗。我为此花了一个星期,请帮助我。
工具栏.控制器
(function ()
{
'use strict';
angular
.module('app.toolbar')
.controller('ToolbarController', ToolbarController);
function ToolbarController($rootScope, $mdSidenav, msNavFoldService, $translate, $mdToast, $location, $localStorage, $http, $scope)
{
var vm = this;
vm.name = $localStorage.name;
vm.userId = $localStorage._id;
vm.readNotifications = function(notifId){
$http({
url: 'http://192.168.2.8:7200/api/readNotification',
method: 'POST',
data: {notificationId: notifId, userId: vm.userId}
}).then(function(res){
vm.rslt = res.data.result1;
console.log(vm.rslt);
vm.refresh();
$location.path('/sharedwishlistdetails');
}, function(error){
alert(error.data);
})
}
}
})();
这里存储的数据在vm.reslt中。
工具栏.module.js
(function ()
{
'use strict';
angular
.module('app.toolbar', [])
.config(config);
/** @ngInject */
function config($stateProvider, $translatePartialLoaderProvider)
{
$translatePartialLoaderProvider.addPart('app/toolbar');
}
})();
现在我想要这个控制器的结果。
sharedwishlistdetails.controller.js
(function ()
{
'use strict';
angular
.module('app.sharedwishlistdetails')
.controller('SharedWishlistDetailsController', SharedWishlistDetailsController);
/** @ngInject */
//NotificationsController.$inject = ['$http', '$location'];
function SharedWishlistDetailsController($http, $location, $localStorage, $rootScope, $scope)
{
var vm = this;
vm.uid = $localStorage._id;
}
})();
shareddata.service.js
(function ()
{
'use strict';
angular
.module('app.core')
.factory('shareData', shareDataService);
/** @ngInject */
function shareDataService($resource,$http) {
var shareData = {};
return shareData;
}
})();
【问题讨论】:
-
你试过角服务/工厂吗?
-
您有两种方法可以做到这一点: 1. 使用作为单例的服务/工厂。 2. $rootScope
-
我已经展示了一个我正在使用该服务的类型的链接。
-
检查我的答案here希望你得到你想要的东西
标签: angularjs node.js mongodb express