2014-10-29 137 views
6

我在快樂Angularjs創建Web應用程序,我寫在日誌單獨的.js文件的代碼從數據庫 是爲執行頁面加載但不觸發按鈕點擊, 我的.js代碼:ng按鈕事件不會觸發按鈕單擊?

var adminModule = angular.module('angApp', []); 
    //Defining a Angular Controller 
    adminModule.controller('AdminCtrl', ['$scope', '$http', 
    `enter code here` function ($scope, $http) { 
    Login(); 
      function Login(U_Name, U_PWD) { 
     debugger; 
     //Defining the $http service for login the admin user 
     $http({ 
      method: 'POST', 
      url: '/Admin/IsAuthenticate', 
      data: { User_Name: U_Name, User_PWD: U_PWD } 
     }).success(function (result) { 

      if (result == true) { 
       alert('user is valid'); 
      } 
      else { 
       alert('unauthorised access!'); 
      } 
     }) 
      .error(function (error) { 
       //Showing error message 
       $scope.status = 'Unable to connect' + error.message; 
      }); 
    } 
     }]); 

和我,因爲我使用的是什麼在這裏結合這一點使用Angularjs看法是上面的代碼中的一個問題是工作在頁面加載,但不要點擊按鈕的工作,我使用的代碼是:

<div class="admin-login" ng-controller="AdminCtrl" ng-app="angApp"> 
    <h2>using angularjs</h2> 
    <input type="text" id="txtUserAng" placeholder="User Name" ng-model="U_Name" /> 
    <input type="password" id="txtPWDAng" placeholder="Password" ng-model="U_PWD" /> 
    <input type="button" id="login" value="login" ng-click="Login()" /> 
</div> 

請人幫我看看我在這裏錯過了什麼,所以我不能觸發ng-click事件按鈕點擊 在此先感謝。

+0

應該scope.login $ =()函數等 – user2717954 2014-10-29 15:10:05

+0

我用它,但它不可能解決我的問題: $ scope.login = 功能的Logi N(U_Name,U_PWD){ $ HTTP({ 方法: 'POST', URL: '/管理/ IsAuthenticate', 數據:{的User_Name:U_Name,User_PWD:U_PWD} })。成功(函數(結果){ if(result == true){ alert('user is valid'); } else { alert('unauthorized access!'); } }) } – 2014-10-29 15:11:03

+1

然後顯示在主線程中更新的代碼 – user2717954 2014-10-29 15:13:11

回答

14

您的Login函數需要在範圍內。現在,它基本上是一個私有函數:

$scope.Login = function() { 
    ... 
} 
+0

我正在寫上面一樣,但它不是在單擊事件執行它的正常工作負載 – 2014-10-29 15:20:36

+0

它的工作負載,因爲控制器在創建時在其詞法範圍內調用Login()。你可以創建一個Plunk或JsFiddle來顯示你的問題?如果你在範圍上定義函數,然後在你的HTML中引用它,它應該工作 – sma 2014-10-29 15:21:48

+0

請檢查jsfiddle: http://jsfiddle.net/amsharma/pefLcbua/ – 2014-10-29 15:28:41

3

將函數賦值給作用域變量。

var adminModule = angular.module('angApp', []); 
    //Defining a Angular Controller 
    adminModule.controller('AdminCtrl', ['$scope', '$http', 
    function ($scope, $http) { 

      $scope.Login=function (U_Name, U_PWD) { 
     debugger; 
     //Defining the $http service for login the admin user 
     $http({ 
      method: 'POST', 
      url: '/Admin/IsAuthenticate', 
      data: { User_Name: U_Name, User_PWD: U_PWD } 
     }).success(function (result) { 

      if (result == true) { 
       alert('user is valid'); 
      } 
      else { 
       alert('unauthorised access!'); 
      } 
     }) 
      .error(function (error) { 
       //Showing error message 
       $scope.status = 'Unable to connect' + error.message; 
      }); 
    } 
     $scope.Login(); 
     }]); 

編輯:

嘗試使用這個HTML代碼,但我不sure.it可能是,NG-init和NG-控制器都在同一個div和NG-控制器負載第一該NG-初始化後:

<div ng-app="angApp"> 
    <div class="admin-login" ng-controller="AdminCtrl" > 
    <h2>using angularjs</h2> 
    <input type="text" id="txtUserAng" placeholder="User Name" ng-model="U_Name" /> 
    <input type="password" id="txtPWDAng" placeholder="Password" ng-model="U_PWD" /> 
    <input type="button" id="login" value="login" ng-click="Login()" /> 
    </div> 
</div> 
+0

我正在寫與上述相同,但它不是在單擊事件上執行 它正在工作加載好 – 2014-10-29 15:20:01

+0

試試上面的html代碼。 – 2014-10-29 15:30:56

+0

問題只有ng-click事件它在加載時工作正常,但點擊事件它不會去.js控制器 – 2014-10-29 15:36:42