2016-03-28 45 views
0

我有一個頁面內的登錄表單,它有自己的控制器來處理登錄功能,但由於某種原因登錄功能沒有被提交的表單被解僱。AngularJS方法在控制器分配給Div不工作

較大視圖文件中的形式:

<div class="login-wrap" ng-controller="LoginCtrl as logincontrol"> 

     <form ng-class="{submited: submited}" class="form-login" novalidate name="loginForm" x-ng-submit="logincontrol.login()"> 

      <div class="form-group" ng-class="{errorEmail: loginForm.email.$error.required || loginForm.email.$error.email}"> 
       <input type="email" name="email" id="email" class="form-control login-form-input" placeholder="{{'Email' | translate}}" x-ng-model="username" required> 
      </div> 

      <div class="form-group" ng-class="{errorPassword: loginForm.password.$error.required}"> 
       <input type="password" id="password" name="password" class="form-control login-form-input" placeholder="{{'Password' | translate}}" x-ng-model="password" required> 
      </div> 

      <div class="text-center"> 
       <button class="btn btn-success login-button" type="submit" translate>SIGN IN</button> 
      </div> 

     </form> 

</div> 

登錄控制:

angular.module('services.security') 
    .controller('LoginCtrl', function ($scope, securityService, $rootScope) { 
     $scope.messages = { 
      error: false 
     }; 

     console.log("HERE"); 

     $scope.submited = false; 

     $scope.loading = false; 

     var validateEmail = function(email) { 
      var pattern = /^(([^<>()[\]\\.,;:\[email protected]\"]+(\.[^<>()[\]\\.,;:\[email protected]\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; 

      return email.match(pattern); 
     }; 

     $scope.login = function() { 
      console.log("clicked"); 

      $scope.submited = true; 

      if (!$scope.loginForm.email.$viewValue) { 
       $scope.loginForm.email.$dirty = true; 
       $scope.loginForm.email.$invalid = true; 
       $scope.loginForm.email.$error.required = true; 
      } 

      if (!$scope.loginForm.password.$viewValue) { 
       $scope.loginForm.password.$dirty = true; 
       $scope.loginForm.password.$invalid = true; 
       $scope.loginForm.password.$error.required = true; 
      } 

      if (validateEmail($scope.loginForm.email.$viewValue)) { 
       if ($scope.loginForm.$valid) { 
        $scope.messages.error = false; 
        $scope.loading = true; 

        securityService.login($scope.loginForm.email.$viewValue, $scope.loginForm.password.$viewValue).then(function() { 
         $scope.loading = false; 
        }, function(data) { 
         console.log(data) 
         $scope.loading = false;; 
        }); 
       } 
      } else { 

       if ($scope.loginForm.email.$viewValue){ 
        $scope.loginForm.email.$dirty = true; 
        $scope.loginForm.email.$invalid = true; 
        $scope.loginForm.email.$error.email = true; 
       } 
      } 

     }; 
    }); 

當登錄視圖被加載HERE被印刷在控制檯,所以所述控制器被裝載但是當提交表單時(例如,clicked未打印),登錄功能不會觸發。

回答

0

您正在通過在HTML代碼中指定LoginCtrl as logincontrol來創建實例。

如果你想與$scope變量使用只需指定ng-controller="LoginCtrl"

替換$scope在他們documentationthis

角度定義這個它說如下:

下面包含兩種不同的聲明樣式:

  • 一個使用this直接結合到控制器的方法和屬性:ng-controller="SettingsController1 as settings"
  • 一個噴射$scope到控制器:ng-controller="SettingsController2"
相關問題