user3386877
user3386877

Reputation: 575

Angular gives error when I am passing parameter in Angular

TypeError: Cannot read property '{{ answer._id }}' of undefined
at Scope.$scope.showAddComment file : questions.client.controller.js:103:70)    

Here's my code:

<a class="btn btn-xs" data-ng-click="showAddComment('{{ answer._id }}');">
  Add Comment
</a><br />
<div class="col-md-12" ng-show="mustShow['{{ answer._id }}']">      
  <form class="form-horizontal" data-ng-submit="giveAnswerComment()" novalidate>
    <fieldset>
      <input type="hidden" data-ng-model="answer" id="answer" name="answer" value="{{answer._id}}">
      <div class="form-group">
        <div class="controls">
          <textarea data-ng-model="comment" id="comment" class="form-control" cols="30" rows="10" placeholder="Comment" required></textarea>
        </div>
      </div>
      <div class="form-group">
        <input type="submit" class="btn btn-default" value="Post Your Comment">
      </div>
      <div data-ng-show="error" class="text-danger">
        <strong data-ng-bind="error"></strong>
      </div>
    </fieldset>
  </form>
</div>



$scope.showAddComment = function(mustShowId) {
    console.log("Before mustShowId = " + mustShowId);
    // 103:70>
    console.log("Before $scope.mustShow[mustShowId] = "+$scope.mustShow[mustShowId]);
    $scope.mustShow[mustShowId] = true;
    console.log("After $scope.mustShow[mustShowId] = "+$scope.mustShow[mustShowId]);

};

Upvotes: 0

Views: 99

Answers (2)

Peter Ashwell
Peter Ashwell

Reputation: 4302

Change your code to be as follows:

<a class="btn btn-xs" data-ng-click="showAddComment(answer._id);">
    Add Comment
</a><br />
<div class="col-md-12" ng-show="mustShow[answer._id]">      

Having the '{{ }}' made it not defined. Read about https://docs.angularjs.org/guide/expression AngularJS expressions

Upvotes: 1

Sergey Romanov
Sergey Romanov

Reputation: 3080

  1. You do not have $scope.mustShow = ... defined
  2. You do not need to use {{}} there. Just mustShow[answer._id]
  3. I think what you Are trying to do is simple ng-show="answer._id"

Upvotes: 1

Related Questions