0

我对每篇使用 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 中访问它。

谢谢

4

1 回答 1

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);
};
于 2013-08-27T07:07:28.373 回答