首页 > angularjs问题

angularjs问题

controller:

angular.module('Authentication')
  
.controller('LoginController',
    ['$scope', '$rootScope', '$location', 'AuthenticationService',
    function ($scope, $rootScope, $location, AuthenticationService) {
        // reset login status
        AuthenticationService.ClearCredentials();
  
        $scope.login = function () {
            $scope.dataLoading = true;
            AuthenticationService.Login($scope.username, $scope.password, function(response) {
                if(response.success) {
                    AuthenticationService.SetCredentials($scope.username, $scope.password);
                    $location.path('/');
                } else {
                    $scope.error = response.message;
                    $scope.dataLoading = false;
                }
            });
        };
    }]); 

service:

angular.module('Authentication')
  
.factory('AuthenticationService',
    ['Base64', '$http', '$cookieStore', '$rootScope', '$timeout',
    function (Base64, $http, $cookieStore, $rootScope, $timeout) {
        var service = {};
 
        service.Login = function (username, password, callback) {
 
            /* Dummy authentication for testing, uses $timeout to simulate api call
             ----------------------------------------------*/
            $timeout(function(){
                var response = { success: username === 'test' && password === 'test' };
                if(!response.success) {
                    response.message = 'Username or password is incorrect';
                }
                callback(response);
            }, 1000);

plunker:http://plnkr.co/edit/H4SVl6?p=preview

初学,在网上看到一个login的例子,有很多地方不明白,请问:
(1)username和password是在哪里定义的?serveice里面为什么直接在参数里就开始用了
(2)service里面用户名密码验证成功以后,callback(response)是什么意思?
(3)这个例子中用了ngroute和ng-view,如果我不想用route而在登陆后整个页面跳转到另一个同目录下的html页面,应该怎么做?


  1. 这里的usernamepassword很可能是通过模板双向绑定而来,也就是ng-model

  2. callback是回调函数的形参,也就是说在调用login函数时,会传入一个另一个函数的引用,以供在login中被调用

  3. $location

【热门文章】
【热门文章】