2014-02-27 125 views
1


比方说,我有类似Twitter的网站填充消息。有一个功能,您可以添加评论消息。AngularJS - 多种形式 - 一个ng模型?

所以,我遍历每个消息并显示评论表单:

<div class="" ng-repeat="msg in messages"> 
    <span>message: {{msg.message}}</span> 
    <span>date {{msg.date}}</span> 
    <form ng-submit=""> 
     <input type="text" ng-model="commentForm.comment"> 
     <button type="submit" value="send"> 
    </form> 
</div> 

的问题是,当我下一个消息类型注释它显示了所有的输入,这不是我想要的行为。

  • 禁用与禁用=“true”其他输入字段没有帮助。

下面是代码:plnkr

回答

4

根据您提供的代码,所有的文字输入绑定到相同的NG-模型变量。所以,当其中一个值发生变化时,它们会被更新。将注释存储为原始对象的一部分可能会更好。

在控制器:

$scope.messages = [ 
    {'message': 'here goes some message', 
     'date': '1-1-2014', 
     'comment':'' 
    }, 
    {'message': 'here goes another message', 
     'date': '2-2-2014', 
     'comment':'' 
    } 
]; 

而在HTML:

<div class="" ng-repeat="msg in messages"> 
     <input type="text" ng-model="msg.comment"> 
</div> 

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

1

您正在使用的的script.js声明的对象commentForm,这是独一无二的。

<span>Comments:</span> 
<form ng-submit=""> 
    <input type="text" ng-model="msg.commentForm"> 
    <button type="submit">send</button 
</form> 
<br> 

将在每个“msg”对象中为您创建一个字段commentForm。