ng重复并访问它的动态ngmodel值

时间:2013-08-27 06:04:26

标签: angularjs angularjs-ng-repeat

我对每一篇用ngrepeat填充的文章都有评论功能。 添加的新评论应更新为评论。

<div ng-repeat="Article in Articles">
    <div class="Description">
        {{Article.Description}}
    </div>

   <div class="commentbox">
         <textarea placeholder="Share your View" ng-model="Article{{Article.Articleid}}" ></textarea>

           <div class="post">
              <a href="javascript: void(0)" ng-Click="AddComment({{Article.Articleid}});" > Article </a>
           </div>
    </div>
</div>

和添加注释的Js函数是

  $scope.AddComment = function(ArticleID){
    $scope.Comments.push({
        ArticleID: ArticleID,
        **ReviewText: $scope.Comment130, //how to acess the model of received ArticleID**
    DateAdded:new Date()
    });

我的问题是如何将动态值设置为ng-model并在jsfunction中访问它。

由于

1 个答案:

答案 0 :(得分:1)

我认为你的绑定在某种程度上搞砸了。这样的事情应该做到这一点。观察收到的对象的控制台,你可以开始用它们做的事情。

http://plnkr.co/edit/ugtbO2

HTML

<div ng-repeat="a in articles" ng-model="article">
    <div class="description">{{a.description}} (id: {{a.id}})</div>
    <div class="commentbox">
         <textarea placeholder="Share your View" ng-model="a.review"></textarea>
         <div class="post">
            <button ng-click="addComment(a)" > Submit </button>
         </div>
    </div>
</div>

JS

$scope.articles = [
        {id: '1',description: 'sdfsdf', review:''},
        {id: '2',description: 'asa334', review:''},
        {id: '3',description: 'dfsdfw3r', review: ''},
        {id: '4',description: 'sdfsdf', review: ''}
    ];


$scope.addComment = function (article) {
  $scope.comments.push({
    ArticleID: article.id,
    ReviewText: article.review,
    DateAdded : new Date()
  });

  console.log($scope.comments);
};