2
時爲true。除非stuff []至少添加了一個項目,否則整個表單不應有效。當用戶在文本框中輸入一個值,然後單擊添加它,然後將值添加到stuff [],只有當此值已添加到stuff []時,如果啓用提交按鈕。但是,只要用戶在沒有點擊添加的情況下在文本框中鍵入任何內容,並且因此沒有東西[],它就會使表單有效並啓用提交按鈕。
<form ng-app="myApp" ng-controller="myCtrl" name="myForm" ng-submit="submit()" novalidate>
<div ng-repeat="things in stuff">
<table><tr><td>{{things}}</td></tr></table>
</div>
<input type="text" name="app" ng-model="input" ng-required="!stuff[0]" />
<button ng-disabled="!input" ng-click="add()">
<span> add</span>
</button>
<input type="submit" value="Submit" ng-disabled="myForm.$invalid" />
<script>
angular.module('myApp', []).controller('myCtrl', function ($scope) {
$scope.stuff = [];
$scope.input = null;
$scope.add = function() {
var l = $scope.stuff.length;
$scope.stuff[l] = $scope.input;
$scope.input = null;
};
$scope.submit = function() {};
});
</script>
</form>