1

为什么我们需要 ng-form 的 ng-model?没有它就无法正常工作 这是来自 ng book 的示例

<form name="forma" ng-submit="cliker()" ng-controller="myCtrl">
  <div ng-repeat="filed in fields" ng-form="ngforma">
        <input  type="text" 
                name="nameOfInput" 
                placeholder="{{filed.placeholder}}"
                ng-model="field.name"
                ng-required="filed.isRequired"/>
    <div ng-show="ngforma.nameOfInput.$dirty && ngforma.nameOfInput.$invalid">Error</div>
    </div>
<button type="submit" ng-disabled="forma.$invalid">Submit All</button>
</form>


</body>

<script>
    angular.module("APP", [])
        .controller("myCtrl", function($scope){
            $scope.fields = [
                {placeholder: "placeholder", isRequired: "true"},
                {placeholder: "placeholder", isRequired: "true"},
                {placeholder: "placeholder", isRequired: "false"}
            ],

            $scope.cliker = function(){
                alert("Yes")
            }

        })  
</script>

它在没有 ng-model 的情况下工作,但 ng-disabled 不起作用。我什至可以在 ng-model 中放入一些随机文本,这样它就可以工作了。

4

0 回答 0