2015-08-31 91 views
0

我是Angular的新手,我試圖創建一個基本的登錄頁面。我使用路由表來到一個頁面(login.html)併爲其分配控制器(LoginController)。不過,我似乎無法從login.html頁面調用登錄功能。AngularJS函數調用

這是我的控制器

function LoginController($scope, Bank_Factory) { 

    $scope.people = []; 

    function init(){ 
     $scope.people = Bank_Factory.getUsers(); 
    } 
    init(); 

    $scope.order = function (var_name) { 
     $scope.sort_by = var_name; 
     $scope.reverse = !$scope.reverse; 
    } 

    $scope.Login = function() { 
     $window.alert("Hi"); 
    } 
} 

LoginController.$inject = ['$scope', 'Bank_Factory']; 
angular.module('Bank').controller('LoginController',LoginController); 

和登錄頁

<p> Welcome to the login page </p> 

<form> 
UserName: <input type="text" ng-model = "input_name" /> 
Password: <input type="password" ng-model="input_pass" /> 
<button ng-click="Login()">Login</button> 
</form> 

這是鏈接的login.html頁和LoginController中

var app = angular.module('Bank', ['ngRoute']); 

app.config(function ($routeProvider){ 


$routeProvider 
    .when('/',{ 
     controller: MainController, 
     templateUrl: 'app/views/Main.html' 
    }) 
    .when('/user/:id',{ 
     controller: UserController, 
     templateUrl: 'app/views/user.html' 
    }) 
    .when('/login',{ 
     controller: LoginController, 
     templateUrl: 'app/views/login.html' 
    }) 
    .otherwise({redirectTo:'/'}); 
}); 

我主頁上的路由表全部顯示

<!DOCTYPE html> 
<html lang="en-US" ng-app = 'Bank'> 
<head> 
    <meta content="text/html;charset=utf-8" http-equiv="Content-Type"> 
    <meta content="utf-8" http-equiv="encoding"> 
</head> 
<body> 


<div ng-view></div> 

<script src="scripts/angular.js"></script> 
<script src="scripts/angular-route.js"></script> 
<script src="app/app.js"></script></body> 
<script src="app/services/BankFactory.js"></script> 
<script src="app/controllers/MainController.js"></script> 
<script src="app/controllers/UserController.js"></script> 
<script src="app/controllers/LoginController.js"></script> 


</html> 

我試過LoginController.Login(),$scope.Login()Login()但他們都沒有達到這個功能。請幫助:(

+0

有無你在Login.html頁面中包含了LoginController –

+0

邏輯控制器在我上面發佈的路由表中鏈接到了它。 –

+0

你是否在html頁面上包含了控制器js文件?你應該在html –

回答

1

什麼也沒有發生,因爲你還沒有注入$window到控制器注意你的函數:

$scope.Login = function() { 
    $window.alert("Hi"); 
} 

所以添加$window作爲依賴於你的控制器:

LoginController.$inject = ['$scope', '$window', 'Bank_Factory']; 
function LoginController($scope, $window, Bank_Factory) { ... }