在 stuff[] 至少添加了一项之前,整个表单不应有效。当用户在文本框中输入一个值然后单击添加它然后将该值添加到 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>