【问题标题】:AngularJS the function called retrieves data but does not assign it to the $scope variableAngularJS 调用的函数检索数据但没有将其分配给 $scope 变量
【发布时间】:2018-07-29 13:58:27
【问题描述】:

我有一个向数据库添加注释的函数,然后如果返回响应,则调用更新 cmets 列表的函数。

奇怪的是,当从页面正常添加评论时,列表会更新,但是当从模态窗口添加时,您不想再更新了,奇怪的是console.log( resp.data) 显示所有 cmets 的完整列表,但不想将其分配给 $scope.comment。

控制器:

$scope.addComment = (comment) => {
    comment.notificationID = $scope.notification._id;

    return ApiService.comment.addComment(comment)
        .then((resp) => {  
            getCommentList();
        });
};



function getCommentList() {
    let notificationID = $scope.notification._id;

    return ApiService.comment.getCommentList({ notificationID })
        .then((resp) => {
            console.log(resp.data);
            $scope.comments = resp.data;
        })
};

模板 Ejs:

在底部页面notification.ejs

    <div class="row">
      <div class="tab-content col-md-6">
        <div id="stream" class="tab-pane">
          1
        </div>

        <div ng-controller="NotificationController" id="comments" class="tab-pane fade in active">
          <article class="comment" ng-repeat="comment in  comments">
            <div class="circle circle--greater comment__left-side">
              B
            </div>
            <div class="comment__right-side">
              <span class="comment__author">{{comment.user}}</span>
              <span class="comment__content">{{comment.description}}
              </span>
            </div> 
          </article>

          <div class="comment__input">
            <input type="text" class="form-control" ng-model="comment.description" placeholder="Napisz komentarz...">
            <button class="btn btn-success pull-right" ng-click="addComment(comment)">Dodaj</button>
          </div>
        </div>

      </div>
    </div>

页面顶部的模板Ejs(点击评论后打开模态窗口)

<div ng-controller="NotificationController" class="modal fade" id="addComment" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title" id="myModalLabel">Dodawanie nowego komentarza</h4>
            </div>
            <div class="modal-body">

                <form novalidate name="form"> 
                    <div class="form-group">
                        <label for="">Komentarz</label> 
                        <textarea name="" id="" cols="30" rows="10" class="form-control" ng-model="comment.description"></textarea>
                    </div>  
                </form>

            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Zamknij</button>
                <button type="button" class="btn btn-primary"  ng-click="addComment(comment)">Dodaj</button>
            </div>
        </div>
    </div>
</div>

问题是为什么调用相同的函数,第 1 位,她更新列表,第 2 位,他下载列表,但不会分配?

【问题讨论】:

  • 每个ng-controller 创建自己的实例。使用服务跨实例共享数据
  • 谢谢,如何知道第一个下载列表的第二个实例?
  • 阅读一下如何使用服务共享数据
  • 模态对话框创建自己的范围,然后在对话框完成或取消时销毁该范围。从对话框返回的数据应该从返回的承诺中解析。

标签: angularjs modal-dialog angularjs-scope


【解决方案1】:

你应该使用 from

 window.scope = $scope;

  app.controller('test',function($scope,....){
   window.scope = $scope;
   .....
 })

使用window.scope = $scope;,您可以访问$scope 到控制器的所有部分。

【讨论】:

    猜你喜欢
    • 2023-03-31
    • 1970-01-01
    • 2019-03-10
    • 1970-01-01
    • 2018-11-24
    • 1970-01-01
    • 2015-01-31
    • 2018-09-03
    • 1970-01-01
    相关资源
    最近更新 更多