如何访问ng-repeat之外的ng-model值?

How to access ng-model values outside of ng-repeat?

我有一个 table,每个 <td> 中都填充了 <input> 个字段,这些字段在 ng-repeat 中重复。在某些情况下,某些输入可能带有 null 值。我想访问 ng-model 值,以检测用户何时更改 !=nullng-model 的值以启用按钮来处理服务器中的数据。 即ng-repeat中的代码:

<tbody>
    <tr ng-repeat="data in resultValue=(vm.ExcelDataDialog) | filter: FilterNullData" class="fade">
        <td style="min-width: 2px !important;width:2px !important">{{$index + 1}}</td>
        <td>
         <input type="text" ng-model="data.Pershkrimi" ng-model-options="{ 'updateOn': 'blur' }" ng-class="{validate_for_creating_new_ofer: data.Pershkrimi != null, input_red: data.Pershkrimi == null}" />
        </td>
        <td>
         <input type="text" ng-model="data.Klienti" ng-model-options="{ 'updateOn': 'blur' }" ng-class="{validate_for_creating_new_ofer: data.Klienti != null, input_red: data.Klienti == null}" />
        </td>
        <td>
        <input type="text" ng-model="data.KodiArtikullit" ng-model-options="{ 'updateOn': 'blur' }" ng-class="{validate_for_creating_new_ofer: data.KodiArtikullit != null, input_red: data.KodiArtikullit == null}" />
         </td>
         <td>
         <input type="number" ng-model="data.Sasia" ng-model-options="{ 'updateOn': 'blur' }" ng-class="{validate_for_creating_new_ofer: data.Sasia != null, input_red: data.Sasia == null}" />
         </td>
         <td>
         <input type="number" ng-model="data.Cmimi" ng-model-options="{ 'updateOn': 'blur' }" ng-class="{validate_for_creating_new_ofer: data.Cmimi != null, input_red: data.Cmimi == null}" />
         </td>
          <td>
          <input type="text" ng-model="data.DateFillimi" ng-model-options="{ 'updateOn': 'blur' }" ng-class="{validate_for_creating_new_ofer: data.DateFillimi != null, input_red: data.DateFillimi == null}" />
          </td>
           <td>
           <input type="text" ng-model="data.DateMbarimi" ng-model-options="{ 'updateOn': 'blur' }" ng-class="{validate_for_creating_new_ofer: data.DateMbarimi != null, input_red: data.DateMbarimi == null}" />
           </td>
    </tr>
</tbody>

我检查控制器中的数据是否为空,但我想检查它们何时在视图中归档以启用按钮。 有按钮代码:

 <md-button type="submit" id="create_offer"
            ng-click="vm.AddOfferByImportingExcel()"
            class="send-button md-accent md-raised"
            aria-label="krijo_oferte" 
            ng-disabled = "data.Pershkrimi == null">
   Krijo ofertë
 </md-button>

可能是范围问题。如果不了解控制器是如何定义的以及指令是如何在模板中使用的,就很难准确判断问题所在。如果您使用的是 ng-repeat,它将把范围隔离到正在循环的范围内。

ng-repeat='data in datas'

只会在 element/directive 的上下文中提供数据。如果您需要其他可用变量,则需要

ng-repeat='data in datas' other-variable='anotherVarYouNeed'

验证输入并在任何值为空/空时禁用按钮的最简单方法是添加 angular 表单验证。您需要做的就是用一个表单包围您的代码(如果尚未完成)并确保它有一个 name。然后将 ng-required="true" 添加到您想要的每个输入中。然后您可以访问 formName.$invalid 来确定表单是否有效。

<form name="formName">

    <tbody>
        <tr ng-repeat="data in resultValue=(vm.ExcelDataDialog) | filter: FilterNullData" class="fade">
            <td style="min-width: 2px !important;width:2px !important">{{$index + 1}}</td>
            <td>
                <input type="text" ng-model="data.Pershkrimi" ng-model-options="{ 'updateOn': 'blur' }" ng-required="true" ng-class="{validate_for_creating_new_ofer: data.Pershkrimi != null, input_red: data.Pershkrimi == null}" />
            </td>
            <td>
                <input type="text" ng-model="data.Klienti" ng-model-options="{ 'updateOn': 'blur' }" ng-required="true" ng-class="{validate_for_creating_new_ofer: data.Klienti != null, input_red: data.Klienti == null}" />
            </td>
            <td>
                <input type="text" ng-model="data.KodiArtikullit" ng-model-options="{ 'updateOn': 'blur' }" ng-required="true" ng-class="{validate_for_creating_new_ofer: data.KodiArtikullit != null, input_red: data.KodiArtikullit == null}" />
            </td>
            <td>
                <input type="number" ng-model="data.Sasia" ng-model-options="{ 'updateOn': 'blur' }" ng-required="true" ng-class="{validate_for_creating_new_ofer: data.Sasia != null, input_red: data.Sasia == null}" />
            </td>
            <td>
                <input type="number" ng-model="data.Cmimi" ng-model-options="{ 'updateOn': 'blur' }" ng-required="true" ng-class="{validate_for_creating_new_ofer: data.Cmimi != null, input_red: data.Cmimi == null}" />
            </td>
            <td>
                <input type="text" ng-model="data.DateFillimi" ng-model-options="{ 'updateOn': 'blur' }" ng-required="true" ng-class="{validate_for_creating_new_ofer: data.DateFillimi != null, input_red: data.DateFillimi == null}" />
            </td>
            <td>
                <input type="text" ng-model="data.DateMbarimi" ng-model-options="{ 'updateOn': 'blur' }" ng-required="true" ng-class="{validate_for_creating_new_ofer: data.DateMbarimi != null, input_red: data.DateMbarimi == null}" />
            </td>
        </tr>
    </tbody>

    <md-button type="submit" id="create_offer"
        ng-click="vm.AddOfferByImportingExcel()"
        class="send-button md-accent md-raised"
        aria-label="krijo_oferte" 
        ng-disabled = "formName.$invalid">
        Krijo ofertë
    </md-button>

</form>