2015-05-07 125 views
1

我正在嘗試使用驗證形式進入ui-bootstrap模式,並將模式用作取消按鈕,該按鈕在單擊時忽略視圖。如果存在驗證錯誤,則取消不起作用。如果我再次點擊按鈕,那麼模式關閉。我可能做錯了什麼? http://plnkr.co/edit/loFUvJRfuycxd3qQwMgM?p=previewAngularJs表單驗證ui-bootstrap模式

angular.module('myApp', ['ui.bootstrap']); 
 
angular.module('myApp').controller('TestCTRL', function ($scope,modalService) { 
 
\t $scope.login = function() { 
 

 
\t   var modalOptions = { 
 
\t    closeButtonText: 'Cancel', 
 
\t    submitForm : function(form) { 
 
\t    \t if(form.$valid) { 
 
\t \t \t \t   console.log('Loggin in'); 
 
\t \t \t \t  } 
 
\t    } 
 
\t   }; 
 

 
\t   modalService.showModal({}, modalOptions).then(function (result) { 
 
\t    console.log('completed'); 
 
\t   }); 
 
\t  }; 
 
}); 
 

 
angular.module('myApp').service('modalService', function ($modal) { 
 

 
     var modalDefaults = { 
 
      backdrop: true, 
 
      keyboard: true, 
 
      modalFade: true, 
 
      templateUrl: 'login.html' 
 
     }; 
 

 
     var modalOptions = { 
 
      closeButtonText: 'Close', 
 
      actionButtonText: 'OK', 
 
      headerText: 'Proceed?', 
 
      bodyText: 'Perform this action?' 
 
     }; 
 

 
     this.showModal = function (customModalDefaults, customModalOptions) { 
 
      if (!customModalDefaults) customModalDefaults = {}; 
 
      customModalDefaults.backdrop = 'static'; 
 
      return this.show(customModalDefaults, customModalOptions); 
 
     }; 
 

 
     this.show = function (customModalDefaults, customModalOptions) { 
 
      //Create temp objects to work with since we're in a singleton service 
 
      var tempModalDefaults = {}; 
 
      var tempModalOptions = {}; 
 

 
      //Map angular-ui modal custom defaults to modal defaults defined in service 
 
      angular.extend(tempModalDefaults, modalDefaults, customModalDefaults); 
 

 
      //Map modal.html $scope custom properties to defaults defined in service 
 
      angular.extend(tempModalOptions, modalOptions, customModalOptions); 
 

 
      if (!tempModalDefaults.controller) { 
 
       tempModalDefaults.controller = function ($scope, $modalInstance) { 
 
        $scope.modalOptions = tempModalOptions; 
 
        $scope.modalOptions.ok = function (result) { 
 
         $modalInstance.close(result); 
 
        }; 
 
        $scope.modalOptions.close = function (result) { 
 
         $modalInstance.dismiss('cancel'); 
 
        }; 
 
       } 
 
      } 
 

 
      return $modal.open(tempModalDefaults).result; 
 
     }; 
 

 
    });
<!DOCTYPE html> 
 
<html> 
 
<head > 
 
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css"> 
 
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script> 
 
<script src="ui-bootstrap.js"></script> 
 
    <meta charset="utf-8"> 
 
    <title>test</title> 
 
</head> 
 

 
<body ng-app="myApp"> 
 
    <div ng-controller="TestCTRL"> 
 
    <button type="button" ng-click="login()">Login</button> 
 
    </div> 
 
<script src="app.js"></script> 
 
</body> 
 
</html>

<div class="container"> 
 
    <div class="row"> 
 
    <div class="col-sm-6 col-md-4 col-md-offset-4"> 
 
     <div class="account-wall"> 
 
     <h1 class="text-center login-title">Login</h1> 
 
     <form name="loginForm" ng-submit="modalOptions.submitForm(loginForm)" class="form-signin" novalidate> 
 
      <div class="form-group"> 
 
      <label for="email" class="control-label">Email</label> 
 
      <input id="email" type="email" name="email" placeholder="Email" 
 
        class="form-control" ng-model="user.email" required autofocus> 
 
      <div ng-messages="loginForm.email.$error" ng-if="loginForm.$submitted || loginForm.email.$touched" class="errors"> 
 
       <div ng-message="required">Value required</div> 
 
       <div ng-message="email">Valid email required</div> 
 
      </div> 
 
      </div> 
 
      <div class="form-group"> 
 
      <label for="password" class="control-label">Password</label> 
 
      <input id="password" type="password" name="password" placeholder="Password" 
 
        class="form-control" ng-model="user.password" required> 
 
      <div ng-messages="loginForm.password.$error" ng-if="loginForm.$submitted || loginForm.email.$touched" class="errors"> 
 
       <div ng-message="required">password is required</div> 
 
      </div> 
 
      </div> 
 

 
      <button class="btn btn-lg btn-primary btn-block" type="submit"> 
 
      Login 
 
      </button> 
 
      <button class="btn btn-lg btn-primary btn-block" type="button" ng-click="modalOptions.close()"> 
 
      {{modalOptions.closeButtonText}} 
 
      </button> 
 
     </form> 
 
     </div> 
 
    </div> 
 
    </div> 
 
</div>

非常感謝您的幫助。

+0

你已經解決了這個問題解決了嗎? – Luka

回答

0

我通過關閉模式之前的形式設置質樸,在取消動作功能

function _cancel() 
{ 
    $scope.newForm.$setPristine(); 
    $modalInstance.close(); 
}