将数据从登录控制器传递到父级。 Angularjs父控制器没有实例化?

时间:2014-12-04 11:40:02

标签: javascript angularjs

问题:使用accountController登录应用程序时,会设置currentLocation属性。但是,父控制器无法捕获这些更改。我怀疑它只是在我启动Batarang范围检查器后才在indexController中的chrome命中断点中实例化为调试。

[indexController.js]

app.controller('indexController', ['$scope', 'SessionService', function($scope, SessionService) {
        $scope.currentLocation = SessionService.currentLocation;

        $scope.logout = function() {
            accountService.logOut();
            $location.path('/account/login');
        };
    }
}]);

[accountController.js]

angular.module('app').controller('accountController', [
    '$scope', '$rootScope', 'accountService', 'SessionService', 'Location', function ($scope, $rootScope, accountService, SessionService, Location) {
    $scope.user = {
        userName: "",
        password: ""
    };

    $scope.login = function() {
        var userData = $scope.user;

        var result = accountService.login(userData);

        result.then(
            function(data) {
                $rootScope.userLoginStatus = "loginSuccess";
                $rootScope.userName = userData.userName;
                $rootScope.system = {};
                Location.query(function(locations) {
                    $rootScope.system.locations = locations;
                    SessionService.currentLocation.id = $rootScope.system.locations[0].id;
                    SessionService.currentLocation.locationName = $rootScope.system.locations[0].locationName;
                    SessionService.userName = userData.userName;
                    $location.path("/");
                });
            });
    };
}
]);

[sessionService.js]

'use strict';
app.factory('SessionService', ['$rootScope', 'Location', function ($rootScope, Location) {
    var sessionService = {};

    var _currentLocation = { id: 0, locationName: "No location" };


    sessionService.currentLocation = _currentLocation;
    sessionService.userName = "";

    return sessionService;
}]);

这是我的观点

<body ng-controller="indexController">
<div class="navbar-collapse collapse">
             <ul class="nav navbar-nav">

                <ul class="nav navbar-nav navbar-right" >
                    <li class="dropdown" ng-show="locations">
                        <a href="" class="dropdown-toggle" data-toggle="dropdown">{{currentLocation.locationName}}<span class="caret"></span></a>
                    </li>
                    <li class="dropdown" ng-show="userLoginStatus">
                        <a href="" class="dropdown-toggle" data-toggle="dropdown">{{userName}}<span class="caret"></span></a>
                        <ul class="dropdown-menu" role="menu">
                            <li><a ng-click="logout()">Logout</a></li>
                        </ul>
                    </li>
                </ul>
             </ul>


<div class="container body-content" ng-view>
        @RenderBody()
    </div>
</body>

以下是我的路由的一部分:

[app.js]

app.config([
    '$routeProvider', '$locationProvider', function ($routeProvider, $locationProvider) {

        $routeProvider
            .when('/', {
                templateUrl: 'app/components/home/home.html',
                controller: 'homeController'
            })
            .when('/account/login', {
                templateUrl: 'app/components/account/login.html',
                controller: 'accountController'
            })
            //routes omitted for brevity
            .when('/404', {
                templateUrl: 'app/components/404.html'
            })
            .otherwise({
                redirectTo: '/404'
            });

在accountController中更新currentLocation后,Indexcontroller不会更新视图模型。我注意到注销按钮也不起作用

0 个答案:

没有答案