javascript - Angular 双向绑定(bind)似乎不起作用,而文档说它应该起作用

标签 javascript angularjs

我正在尝试使用 $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>

骗子:http://plnkr.co/edit/lG0Ckjctsj9Hu83lTydh?p=preview

最佳答案

addComment 方法中使用 this.commentText=null; 代替 $scope.commentText=null

更新了您的plunkr

编辑:当我注意到这里有一个很好的解释时,我开始输入解释: 'this' vs $scope in AngularJS controllers

关于javascript - Angular 双向绑定(bind)似乎不起作用,而文档说它应该起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24420699/

相关文章:

javascript - 如何在 Application.cfc 的 onRequestStart() 中包含 JavaScript

javascript - WooCommerce API 授权在 node.js 应用程序中不起作用

javascript - Angular js - 从 Controller 调用 data-modal-target

javascript - ng-repeat 只添加一次

javascript - 模态搜索过滤器不会清除 ng-repeat 列表,以前的结果仍然存在

javascript - 取决于两个 promise

JavaScript 动画占位符不起作用

php - 堆栈溢出错误

javascript - 为什么 Mag Pagination 和 Mag Sort 不起作用

javascript - 同一数组中的 javascript 对象之间的一对一关系 (AngularJS)