我正在尝试使用 $scope.commentText 的值,但是当我调用 addComment() 时,作用域变量为空,尽管它已绑定(bind)。作为解决方法,我将 commentext 作为有效的参数值传递,但它仍然应该有效。我的问题是如何清除绑定(bind)到文本输入的 commentText ...但它也无法按预期工作。我环顾四周......我错过了一些东西,因为我正在按照文档告诉我如何做。那么...有人吗?
$scope.user = "WM";
$scope.commentText='';
$scope.addComment = function(plan, commentText) {
console.log(commentText)
plan.comments.push({text:commentText, user:$scope.user);
commentText=null;
$scope.commentText=null;
};
和 View :
<form ng-submit="addComment(plan, commentText)">
<div class="input-group">
<input class="form-control" type="text" ng-model="commentText" size="30" placeholder="add new comment here">
<span class="input-group-btn">
<input class="btn btn-primary" type="submit" value="add">
</span>
</div>
</form>
最佳答案
在 addComment
方法中使用 this.commentText=null;
代替 $scope.commentText=null
。
更新了您的plunkr
编辑:当我注意到这里有一个很好的解释时,我开始输入解释: 'this' vs $scope in AngularJS controllers
关于javascript - Angular 双向绑定(bind)似乎不起作用,而文档说它应该起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24420699/