当我在Angular中传递参数时,Angular会出错

时间:2015-01-03 06:34:22

标签: angularjs angular

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

这是我的代码:

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

};

2 个答案:

答案 0 :(得分:1)

将您的代码更改为:

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

拥有&#39; {{}}&#39;使它没有定义。阅读https://docs.angularjs.org/guide/expression AngularJS表达式

答案 1 :(得分:1)

  1. 您没有$scope.mustShow = ...已定义
  2. 您不需要在那里使用{{}}。只需mustShow[answer._id]
  3. 我认为你想做的事情很简单ng-show="answer._id"