nikhil reddy
nikhil reddy

Reputation: 96

dynamic ngmodel value in ngrepeat and acessing it

I have comment feature for every article which is getting populated with ngrepeat. New comment added should be updated to comments.

<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>

and the Js function to add the comment is

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

my question is how to set the dynamic value to ng-model and access it in the jsfunction.

Thanks

Upvotes: 0

Views: 2232

Answers (1)

xam.esurk
xam.esurk

Reputation: 133

i think your binding is somehow messed up. Something like this should do it. Watch the console for the received objects and you can start doing stuff with them.

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);
};

Upvotes: 1

Related Questions