【问题标题】:Angular ng-messages inside custom directive inside ng-repeat wrong behaviourng-repeat 错误行为中的自定义指令内的 Angular ng-messages
【发布时间】:2016-11-11 11:56:29
【问题描述】:

我遇到了与本主题中描述的相同的问题: Angular JS ng-message inside directive

不幸的是,该解决方案对我不起作用,因为我在 ng-repeat 中使用了我的自定义指令。所以现在 ng-messages 仅在所有输入都满足错误条件时才起作用。

http://plnkr.co/edit/lJT48bmYvR9DGFliIYDH?p=preview

我尝试了很多方法来创建 ng-messages 条件,但没有一个能正常工作。其中两个你可以在上面的 plunker 中找到:

ng-messages="form.doubleInputLeft.$error"
ng-messages="form['doubleInputRight' + $index].$error" 

请帮帮我, 问候

【问题讨论】:

标签: html angularjs angularjs-directive angularjs-ng-repeat ng-messages


【解决方案1】:

要使input 元素在ng-repeat 中工作,必须将索引作为名称属性的一部分。

    <!-- index must be included in the name attribute --
    <input name="doubleInputLeft" class="form-control ngMessageSample" type="{{inputType}}"  ng-model="modelLeft" ng-minlength="2" ng-maxlength="20" required>
    -->
    <input name="doubleInputLeft{{index}}" class="form-control ngMessageSample" type="{{inputType}}"  ng-model="modelLeft" ng-minlength="2" ng-maxlength="20" required>
    <div ng-messages="form['doubleInputLeft'+index].$error" class="ngMessagesClass" ng-messages-multiple>
      <div ng-message="minlength" class="ngMessageClass"> {{leftInputHeading}} must have at least 2 characters.</div>
      <div ng-message="maxlength" class="ngMessageClass"> {{leftInputHeading}} must have at most 20 characters.</div>
    </div>

否则,重复的元素将具有重复的名称。

DEMO on PLNKR

【讨论】:

  • 非常感谢@georgeawg。那是我想的最后一件事。我还将更改指令名称。再次感谢您!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-05
  • 1970-01-01
  • 2017-11-01
相关资源
最近更新 更多