0
我有兩個文本字段:月和年。我不希望用戶輸入未來的月/年組合(即大於當前月份/年)。驗證比較兩個字段
HTML:
<input type="text" ng-model="vm.month" ng-blur="doThing()" required/>
<input type="text" ng-model="vm.year" ng-blur="doThing()" required/>
的Javascript:
$scope.doThing = function() {
if (inFuture) {
// set form to invalid, blah blah
}
}
這個領域已經是一個指令裏面,我想,以避免產生額外的指令。
doThing()
必須做什麼來設置表單的有效性?
向我們顯示您的表單的html,但基本上您必須爲表單分配一個$ scope變量並使用表單驗證:https:// docs .angularjs.org /導向/形式。你也可以在'doThing()' – cl3m
中加上'required =「a_scope_variable」',你可以設置爲'true'或'false',只需要使用ng-change並且檢查這個值是否符合你的需要。這將是IMO最簡單的解決方案 –