简体   繁体   English

ng-disabled无法正常工作

[英]ng-disabled is not working

This might be a silly question. 这可能是一个愚蠢的问题。 But I just started experimenting with AngularJS. 但是我刚刚开始尝试AngularJS。 Can someone point out what I'm doing wrong in validation. 有人可以指出我在验证中做错了什么。 The button is not disabled even when the fields are invalid. 即使字段无效,也不会禁用该按钮。

What's the difference between ng-disabled="myForm.$invalid" and ng-disabled="myForm.$invalid"

ng-disabled="myForm.cuisine.$invalid || myForm.title.$invalid || myForm.description.$invalid || myForm.cost.$invalid"

I think using myForm.$invalid is a cleaner way of disabling the button. 我认为使用myForm.$invalid是禁用按钮的更干净的方法。 Any tips? 有小费吗?

<form name="myForm">
    <div class="form-group">
        <select ng-required="true" name="cuisine" ng-model="model.food.cuisine" class="form-control" type="text">
            <option ng-repeat="c in model.cuisines" value="{{c}}">{{c}}</option>
        </select>
        <p ng-show="myForm.cuisine.$invalid && myForm.cuisine.$touched">
            Please select cuisine type.</p>
    </div>

    <div class="form-group">
        <input ng-required="true" name="title" ng-minlength="4" ng-maxlength="25" ng-model="model.food.title"
               type="text" class="form-control">
        <p ng-show="myForm.title.$invalid && myForm.title.$touched">Please pick a valid title with atleast 4
            characters.</p>
    </div>

    <div class="form-group">
            <textarea ng-required="true" name="description" ng-minlength="10" ng-maxlength="255"
                      ng-model="model.food.description" type="text" class="form-control"></textarea>
        <p ng-show="myForm.description.$valid && myForm.description.$touched">Please describe your food with 10 - 255
            characters.</p>
    </div>

    <div class="form-group">
        <input name="cost" ng-pattern="/0-9/" ng-required="true" min="1" ng-model="model.food.cost" type="number"
               class="form-control" placeholder="Cost">
        <p ng-show="myForm.cost.$error.pattern">Please enter a valid number</p>
        <p ng-show="myForm.cost.$invalid && myForm.cost.$touched">Please enter cost of food item.</p>
    </div>

    <div class="form-group">
        <button ng-disabled="myForm.$invalid" class="btn btn-success btn-block">Add Food</button>
    </div>
</form>

Apparently, validations don't work if the form element is inside the body of a table. 显然,如果form元素位于表主体内部,则验证不起作用。

It works fine if I moved the form tag outside the table. 如果将表格标签移到表格外,效果很好。

Thanks everyone. 感谢大家。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM