AngularJS重置表單 ng-dirty

<div ng-app="myApp" ng-controller="myCtrl as ctrl">
      <form name="ctrl.myForm">
        <div><label for="email">Email</label>
        <input name="myInput" type="email" ng-model="ctrl.email" id="email" required></div>
        <div><label for="password">Password</label>
        <input name="myPassword" type="password" minlength="8" ng-model="ctrl.password" id="password" required></div>
        <div>
        <button ng-click="ctrl.reset()" type="button">Reset</button>
        </div>
      </form>
angular.module('myApp', [])
    .controller('myCtrl', myCtrl);  
function myCtrl(){    var vm = this;
    vm.reset = function(){
        vm.myForm.$setPristine();
        vm.myForm.$setUntouched();
        vm.email = vm.password = '';
  }
}

表單input區域必須放在 form

參考連接:Angular clear subform data and reset validation

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章