ng-messages不能与ng-repeat一起使用

时间:2020-12-14 18:56:37

I can't get ng-messages to work with ng-repeat.

我无法使用ng-repeat来处理ng-repeat。

<form name="testForm">
   <input type="text" name="text" ng-model="text" required />
   <div ng-messages="testForm.text.$error">
      <div ng-repeat="Error in errors" ng-message="Error.type">{{ Error.message }} </div>
   </div>
</form>

Here is a example: http://codepen.io/jakej/pen/dXvRdp First form is using ng-repeat.

这是一个例子:http://codepen.io/jakej/pen/dXvRdp第一种形式是使用ng-repeat。

I've tried both ng-message and ng-message-exp but none of them works. Why is ng-repeat breaking ng-message directive?

我已经尝试过ng-message和ng-message-exp,但它们都不起作用。为什么ng-repeat打破ng-message指令?

Thanks in advance!

提前致谢!

3 个解决方案

#1


1  

First you have an error on your Json array.

首先,你的Json数组有错误。

After, you need add a span with ng-message attribute children of the ng-repeat <div>

之后,您需要添加带有ng-repeat

的ng-message属性子句的span

And add {{ }} in your ng-message attribute

并在您的ng-message属性中添加{{}}

It works good

它运作良好

JS

 $scope.errors = [{
   type: "required",
   message: "Field is required"
 }];

HTML

<div ng-repeat="Error in errors">
    <span ng-message="{{Error.type}}">{{Error.message}}</span>
</div>

UPDATE

You don't need to add a children element, only the miss of {{}} was important

您不需要添加子元素,只有{{}}的遗漏很重要

<div ng-repeat="Error in errors" ng-message="{{Error.type}}">                                                                      
    {{Error.message}}
</div>

#2


1  

Instead of using ng-repeat and ng-message together, try this way:

不要一起使用ng-repeat和ng-message,请尝试这种方式:

<form name="testForm">
   <input type="text" name="text" ng-model="text" required />
<div ng-repeat="item in items">
<div ng-messages="testForm.text.$error">
      <div ng-message="Error.type">{{ Error.message }} </div>
   </div>
</div>

</form>

#3


0  

Try to make errors in scope to an array :

尝试在范围内使数组出错:

$scope.errors = [{
   type: "required",
   message: "Field is required"
}]

#1


1  

First you have an error on your Json array.

首先,你的Json数组有错误。

After, you need add a span with ng-message attribute children of the ng-repeat <div>

之后,您需要添加带有ng-repeat

的ng-message属性子句的span

And add {{ }} in your ng-message attribute

并在您的ng-message属性中添加{{}}

It works good

它运作良好

JS

 $scope.errors = [{
   type: "required",
   message: "Field is required"
 }];

HTML

<div ng-repeat="Error in errors">
    <span ng-message="{{Error.type}}">{{Error.message}}</span>
</div>

UPDATE

You don't need to add a children element, only the miss of {{}} was important

您不需要添加子元素,只有{{}}的遗漏很重要

<div ng-repeat="Error in errors" ng-message="{{Error.type}}">                                                                      
    {{Error.message}}
</div>

#2


1  

Instead of using ng-repeat and ng-message together, try this way:

不要一起使用ng-repeat和ng-message,请尝试这种方式:

<form name="testForm">
   <input type="text" name="text" ng-model="text" required />
<div ng-repeat="item in items">
<div ng-messages="testForm.text.$error">
      <div ng-message="Error.type">{{ Error.message }} </div>
   </div>
</div>

</form>

#3


0  

Try to make errors in scope to an array :

尝试在范围内使数组出错:

$scope.errors = [{
   type: "required",
   message: "Field is required"
}]