简体   繁体   English

基于两种不同形式的Ng禁用按钮

[英]Ng-disabled button based on two different forms

While working on a project I came across a problem, I needed to disable a button as long as 2 different tables (with an ng-repeat in it for the rows, which in turn had a required input per row) had empty fields still. 在进行项目时,我遇到一个问题,只要2个不同的表(行中都有ng-repeat,而每行又有一个必需的输入)中仍然有空字段,就需要禁用按钮。 So basically: 所以基本上:

<table class="table table-striped fontsize12">
<thead>
    <tr>
        <th class="width-desc-auto">Description</th>
        <th class="width-reg-auto">Value</th>
    </tr>
</thead>
<tbody>
    <tr ng-repeat="item in items">
        <td><h6>{{item.DESCRIPTION}}</h6></td>
        <td>
            <input type="text" class="form-control" ng-model="item.VALUE" 
                   placeholder="{{item.VALUE}}" disabled="true" required
            >
        </td>
    </tr>
</tbody>

And : 和:

    <table class="table table-striped fontsize12">
<thead>
    <tr>
        <th class="width-desc-auto">Description</th>
        <th class="width-reg-auto">Value</th>
    </tr>
</thead>
<tbody>
    <tr ng-repeat="char in chars">
        <td><h6>{{char.DESCRIPTION}}</h6></td>
        <td>
            <input type="text" class="form-control"
              ng-model="char.VALUE" placeholder="Register Value" required
            >
        </td>
    </tr>
</tbody>

The first one being filled in automatically, but they could still be empty in certain cases and the second one has to be input manually. 第一个自动填写,但是在某些情况下它们仍然可以为空,第二个必须手动输入。 The actual arrays over which I repeat don't really matter. 我重复的实际数组并不重要。 Right now I solved it by putting a form tag surrounding both of the tables and included the button within that form tag aswell.: 现在,我通过在两个表的周围放置一个表单标签并在该表单标签中也包含按钮来解决该问题:

<form name="myForm">
     <table>...</table>
     <table>...</table>
     <span class="pull-right">
         <button class="btn btn-sm btn-primary" type="button" ng-disabled="myForm.$invalid">
           Close
         </button>
     </span>
</form>

This works, but what if I needed both tables to be put in a different form? 这行得通,但是如果我需要将两个表放置为不同的格式怎么办? For example autoForm and manualForm? 例如autoForm和manualForm? And do I need to put the actual button within the form tag aswell? 我是否还需要将实际按钮放在form标签内? As it didn't seem to work if I put it outside of it. 因为如果我把它放在外面似乎没有用。

My apologies if this has been asked before but I couldn't find it. 我很抱歉,是否曾经有人问过我,但我找不到。 Ty in advance. 提前输入。

you don't actually need a form tag as long as the tables are in the same scope . 只要表在同一范围内,您实际上不需要表单标签。 you can just check for the values of both input fields in ng-disabled directive and disable it accordingly for example here you can use 您可以只检查ng-disabled指令中两个输入字段的值,并相应地禁用它,例如,在这里您可以使用

<button class="btn btn-sm btn-primary" type="button" 
        ng-disabled="!(item.value && char.value)"
>
  Close
</button>

In this case button will only enable when both the input types are filled 在这种情况下,仅当两种输入类型都填满时,按钮才会启用

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

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