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]);
};
答案 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)
$scope.mustShow = ...
已定义mustShow[answer._id]
ng-show="answer._id"