从运行块调用$ modal服务时出错

时间:2017-01-03 05:49:03

标签: angularjs

在我的主模块中,我使用了ui-bootstrap模式来检查登录验证。从运行块我调用了loginModal服务,但是它给出了错误

  

TypeError:loginModal不是函数

angular.module('myApp', ['ui.router', 'ui.bootstrap', 'loginServices','leaveServices']). config(['$stateProvider', '$urlRouterProvider', function($stateProvider, $urlRouterProvider) {
        $urlRouterProvider.otherwise('/home');
        $stateProvider.
            state('home', {
                url:'/home',
                data: {
                    requireLogin: false
                },
                views: {
                    '': {
                        templateUrl: 'partials/templates/assets/home.html'
                    }
                }
            }).
            state('home.about', {
                url: '/about',
                templateUrl: 'partials/templates/assets/about.html'
            }).
            state('home.contact', {
                url: '/contact',
                templateUrl: 'partials/templates/assets/contactUs.html'
            }).
            state('/login', {
                url: '/login',
                data: {
                    requireLogin: false
                },
                templateUrl: 'partials/login.html',
                controller: loginUserController
            }).
            state('/register', {
                url:'/register',
                data: {
                    requireLogin: false
                },
                templateUrl: 'partials/registerUser.html',
                controller: registerController
            }).
            state('/getAllUsers', {
                url: '/getAllUsers',
                data: {
                    requireLogin: false
                },
                templateUrl: 'partials/getAllUsers.html',
                controller: getUsersController
            }).
            state('/updateUser', {
                url : '/updateUser/:id/:name',
                data: {
                    requireLogin: true
                },
                params: {'id':null, 'name':null},
                templateUrl: 'partials/updateUser.html',
                controller: updateUserController
            }).
            state('/userLeave', {
                url : '/userLeave:name',
                data: {
                    requireLogin: true
                },
                params: {'name': null},
                templateUrl: 'partials/userLeave.html',
                controller: userLeaveController
            }).
            state('/leaveRequest', {
                url : '/leaveRequest',
                data: {
                    requireLogin: true
                },
                templateUrl: 'partials/leaveRequest.html'
            });
    }])

    .run(function ($rootScope, $state, loginModal) {
    $rootScope.$on('$stateChangeStart', function (event, toState, toParams) {
        var requireLogin = toState.data.requireLogin;
        console.log('going to state '+toState.name);
        if (requireLogin && typeof $rootScope.currentUser === 'undefined') {
            event.preventDefault();

            loginModal().then(function () {
                    return $state.go(toState.name, toParams);
                })
                .catch(function () {
                    return $state.go('/login');
                });
        }
    });
})
.factory('loginModal', function ($modal, $rootScope) {

    function assignCurrentUser (user) {
        $rootScope.currentUser = user;
        return user;
    }

    return function() {
            var instance = $modal.open({
                templateUrl: 'partials/login.html',
                controller: loginUserController
            });

            return instance.result.then(assignCurrentUser);
        }
});

这里登录控制器

loginUserController.$inject = ['$scope','$http', 'loginFactory', '$location', '$state'];
function loginUserController($scope,$http,loginFactory,$location, $state){

$scope.validateLogin = function(name,password){
    $http.get("http://localhost:3010/validateLogin?userName="+name+"&password="+password)
        .then(function(response) {
            if(response.data.length != 0) {
                console.log("logged user data is "+JSON.stringify(response.data));
                $state.transitionTo('/userLeave', {name: name});
           //     $scope.$close(response);
            }
            else
                $scope.inValidUser = 'Invalid User';
        });
};
$scope.cancel = $scope.$dismiss;
}

1 个答案:

答案 0 :(得分:0)

可能是因为你正在使用Angular service,它需要一个构造函数,而应该返回一些东西。

尝试更改为factory,例如

.factory('loginModal', ...