Angular v1.57:angularjs選擇下拉驗證
有一個問題,當我填充我的選擇下拉列表我想驗證它。應該要求並且應該選擇一個項目。當我在我的模型中獲取一些數據並且它不好時,下拉列表不應選擇任何內容。這種方式是開箱即用的,但它應該使我的選擇下拉字段$無效,以便在其周圍繪製簡單的紅色邊框(使用CSS)。我所有的輸入字段都有相同的結構。你可以看到,我已經用下面的plnkr嘗試過了,但沒有運氣。選擇下拉字段保持有效,即使沒有選擇任何內容,但我的模型($ scope.data.selector)具有「falsy」值。
$scope.data = {
selector: 3
}
當我改變模型爲有效的值,e.g:
$scope.data = {
selector: 2
}
我可以看到,在下拉列表中選擇的值。但是當出現「虛假」值時,選擇下拉應該是$無效的。我該如何做到這一點(當沒有值時,它應該像輸入字段一樣)。
http://plnkr.co/edit/unmF87anBrm4q8ZguPMp?p=preview
<body ng-app="myApp" ng-controller="MyController">
<form name="testForm" novalidate="">
<label>Select a number</label>
<div ng-class="{'has-error': testForm.testList.$invalid}">
<select class="form-control" name="testList" ng-model="data.selector" ng-options="item.value as item.label for item in list" required></select>
</div>
<label>Input something</label>
<div ng-class="{'has-error': testForm.testInput.$invalid}">
<input class="form-control" name="testInput" type="text" ng-model="data.inputor" required />
</div>
</form>
</body>
var myApp = angular.module("myApp", []);
myApp.controller("MyController", function($scope) {
$scope.data = {
selector: 3,
inputor: ""
}
$scope.list = [{
value: 1,
label: "One"
}, {
value: 2,
label: "Two"
}];
});
這不會給角度v1.57所需的結果。根據angularjs,所選標籤仍然是有效的。 –