2017-01-15 96 views
1

我發現下面的代碼很奇怪。 dataSvc.saves的回調之後是什麼vm.bookDetails = {}; and vm.bookForm.$setPristine

angular.module('controllers',[]) 
    .controller('SecondController', function(dataSvc){ 
    var vm=this; 

    vm.saveData = function() { 
     dataSvc.save(vm.bookDetails).then(function(result) { 
     vm.bookDetails = {}; 
     vm.bookForm.$setPristine(); 
     }); 
    }; 

    vm.numberPattern = /^\d*$/; 
    }); 

的代碼是從here

回答

1

的NG-髒一流告訴您該表已被用戶修改,而NG-原始類告訴你的形式沒有被修改用戶。所以ng-dirty和ng-pristine是同一故事的兩面。

這些類是在任何字段上設置的,而表單有兩個屬性$ dirty和$ pristine。

Angular.org

$ setPristine意味着

設置形式到它的原始狀態。

該方法將表單的$ pristine狀態設置爲true,$ dirty狀態 爲false,刪除ng-dirty類並添加ng-pristine類。 此外,它將$提交狀態設置爲false。

您可以使用$ scope.form。$ setPristine()函數將表單重置爲pristine狀態。 $ setPristine()在angularjs

代碼說明

angular.module('controllers',[]) 
    .controller('SecondController', function(dataSvc){ 
    var vm=this; 

    vm.saveData = function() { 
     dataSvc.save(vm.bookDetails).then(function(result) { 
     // This code will reset the form/clear value of all ngmodel 
     vm.bookDetails = {}; 
     // This code will set the validity of the form to invalid state, submitted cannot be made if pristine is set. 
     vm.bookForm.$setPristine(); 
     }); 
    }; 

    vm.numberPattern = /^\d*$/; 
    }); 
+0

是不是就意味着重置的1.1.x版本分支引入? –

+0

在您的代碼中,這意味着表單的有效性,當調用setPristine時無法提交表單。 – digit