【问题标题】:I want to share data stored in variable from one controller to another我想将存储在变量中的数据从一个控制器共享到另一个控制器
【发布时间】: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


【解决方案1】:

$rootScope 怎么会在您的代码中失败,如果您粘贴您的代码将不胜感激:没关系,这里有一个可以帮助您的示例:

所有应用程序都有一个 $rootScope,它是在包含 ng-app 指令的 HTML 元素上创建的范围。 rootScope 在整个应用程序中可用。如果一个变量在当前作用域和 rootScope 中具有相同的名称,则应用程序使用当前作用域中的那个。

angular.module('myApp', [])
 .run(function($rootScope) {
    $rootScope.test = new Date();
 })

.controller('myCtrl', function($scope, $rootScope) {
   $scope.change = function() {
      $scope.test = new Date();
   };

 $scope.getOrig = function() {
   return $rootScope.test;
 };
})

.controller('myCtrl2', function($scope, $rootScope) {
    $scope.change = function() {
       $scope.test = new Date();
    };

 $scope.changeRs = function() {
    $rootScope.test = new Date();
 };

 $scope.getOrig = function() {
    return $rootScope.test;
 };

});

【讨论】:

  • 但是我有两个不同的模块
  • 这就是我所说的,如果你能编辑和粘贴你的控制器将不胜感激
【解决方案2】:

在“app.toolbar”模块中编写服务

 angular.module('app.toolbar').service('ServiceA', function() {
     this.getValue = function() {
         return this.myValue;
     };

     this.setValue = function(newValue) {
          this.myValue = newValue;
     }
  });

在你的toolbarController中,注入ServiceA并设置数据——

    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;
      ServiceA.setValue(vm.rslt);
      console.log(vm.rslt);
      vm.refresh();
      $location.path('/sharedwishlistdetails');
     }, function(error){
       alert(error.data);
     })
    }

现在为“app.sharedwishlistdetails”模块编写另一个服务 -

   angular.module('app.sharedwishlistdetails',['app.toolbar']).service('ServiceB', function(ServiceA) {
     this.getValue = function() {
     return ServiceA.getValue();
   };

   this.setValue = function() {
     ServiceA.setValue('New value');
   }
   });

现在在您的 SharedWishlistDetailsController 控制器中注入 ServiceB 并访问数据 -

  var sharedData = ServiceB.getValue();

【讨论】:

  • 我可以为此创建两个服务吗
  • 这里在两个不同的模块中有两个不同的服务..只是为了在两个模块之间共享数据
  • 我完全糊涂了。在我的情况下怎么办
  • 我已经按照你所说的包含了,但是在第二个控制器中它给出了结果undefined
  • 我再次更新了答案。在您的第二个模块中注入第一个模块参考
猜你喜欢
  • 2013-05-21
  • 2020-11-23
  • 1970-01-01
  • 1970-01-01
  • 2017-10-30
  • 2012-09-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多