【问题标题】:pass value from one different app module controller to another app module service using $rootScope in angularjs在 angularjs 中使用 $rootScope 将值从一个不同的应用模块控制器传递到另一个应用模块服务
【发布时间】:2017-01-07 15:46:18
【问题描述】:

我正在使用 angularjs 做项目,我的要求是需要使用 $rootScope 将值从一个不同的应用模块控制器传递到另一个应用模块服务

Here my part of code 

登录模块和控制器

 var loginApp = angular.module('loginApp', [ 'ngCookies' ]);

 loginApp.controller('loginCtrl', function($scope, $cookies,    $cookieStore,
    $rootScope) {
$scope.redirect = function() {
    if ($scope.name == 'admin' && $scope.password == 'admin') {
        $rootScope.loggedInUser = $scope.name;
        window.location = "pages/index.html";
    } else
        alert('User / Password Invalid');
} 

});

这里是我的 app.js 文件

我将登录模块注入到另一个模块中

  var smartCities = angular.module('smartCities', [ 'ngRoute', 'ngAnimate',
    'ui.bootstrap', 'ngTouch', 'ui.grid.exporter', 'ui.grid',
    'ui.grid.selection', 'ui.grid.autoResize', 'ngCookies', 'loginApp' ]);

下面我在这里访问loggedInuser

 smartCities.run(function($rootScope, $location, $cookies, $cookies,
    $cookieStore) {
$rootScope.$on("$routeChangeStart", function(event, next, current) {
    console.log($rootScope.loggedInUser);
    $location.path(next.$$route.originalPath);

});

});

但在控制台中我收到类似的消息

 undifined

请告诉我哪里做错了

【问题讨论】:

    标签: angularjs routes angular-ui-router angular-module angular-route-segment


    【解决方案1】:

    您可以为此目的使用 localstorage 或 sessionStorage。

    登录控制器:

     loginApp.controller('loginCtrl', function($scope, $cookies,    $cookieStore,
        $rootScope) {
     $scope.redirect = function() {
    if ($scope.name == 'admin' && $scope.password == 'admin') {
        localStorage.loggedInUser = $scope.name;
        window.location = "pages/index.html";
    } else
        alert('User / Password Invalid');
    } 
    

    登录用户:

     smartCities.run(function($rootScope, $location, $cookies, $cookies,
    $cookieStore) {
    $rootScope.$on("$routeChangeStart", function(event, next, current) {
    console.log(localStorage.loggedInUser);
    $location.path(next.$$route.originalPath);
    
    });
    

    【讨论】:

    • 不要忘记接受答案,如果它对你有用。
    • 我没有太多意义来接受 Dinesh Shah 的答案。如果我点击向上箭头,它不接受...//
    【解决方案2】:

    这里是文档链接:https://docs.angularjs.org/api/ng/type/angular.Module#value

    //this is one module
    var myUtilModule = angular.module("myUtilModule", []);
    
    // this is value to be shared among modules, it can be any value
    myUtilModule.value  ("myValue"  , "12345");
    
    //this is another module
    var myOtherModule = angular.module("myOtherModule", ['myUtilModule']);
    
    myOtherModule.controller("MyController", function($scope, myValue) {
          // myValue of first module is available here
    }
    
    myOtherModule.factory("myFactory", function(myValue) {
        return "a value: " + myValue;
    });
    

    希望对您有所帮助!

    【讨论】:

    • 感谢重播 Jazib Bashir。我试过这个我无法访问 smartCities.run(function($rootScope, $location, $cookies, $cookies, $cookieStore) { });跨度>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-13
    • 1970-01-01
    • 1970-01-01
    • 2016-01-05
    • 1970-01-01
    • 1970-01-01
    • 2014-07-12
    相关资源
    最近更新 更多