【问题标题】:Reload List after Closing Modal关闭模态后重新加载列表
【发布时间】:2015-08-22 23:13:34
【问题描述】:

我有一个品牌列表:

<script type="text/ng-template" id="list.brands">
        <table class="table table-striped table-bordered bootstrap-datatable datatable dataTable" id="DataTables_Table_0" aria-describedby="DataTables_Table_0_info" ng-controller="BrandsCtrl">
                    <input type="text" ng-model="searchBox">
                    <thead>
                    <tr>
                        <th><tags:label text="brandid"/></th>
                        <th><tags:label text="name"/></th>
                        <th><tags:label text="isactive"/></th>
                        <th></th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr id="actionresult{{$index + 1}}" ng-repeat="brand in brands | filter:searchBox">
                            <td>{{brand.brandid}}</td>
                            <td>{{brand.name}}</td>
                            <td>{{brand.isactive}}</td>

                            <td>
                            <a class="btn btn-ext-darkblue btn-ext-darkblue savestockbtn" ng-click="open(brand.brandid)"><tags:label text="edit"/></a>
                            <a class="btn btn-ext-darkblue btn-modal-trigger btn-ext-darkblue savestockbtn" href="/admin.brands/deleteConfirm?brandid={{brand.brandid}}" data-toggle="modal" ><tags:label text="delete"/></a>
                            </td>
                        </tr>
                    </tbody>
                </table>
    </script>

此列表有品牌行,带有 2 个按钮;编辑和删除。编辑按钮打开品牌编辑表单的模态:

<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<%@taglib tagdir="/WEB-INF/tags" prefix="tags"%>
<%@taglib uri="/WEB-INF/tlds/fields.tld" prefix="fields"%>

 <div class="row-fluid sortable">
    <div class="box span12">
        <div class="box-content">
        <form class="form-horizontal" name="brandform" action='/admin.brands/update' data-toggle="validate" method="post">
            <fields:form formName="brand.id.form"> 
                <input type="hidden" ng-model="item.brandid" name="brandid"/>
            </fields:form> 
            <fields:form formName="brand.form">  
                <div class="section-heading"></div>
                <div class="control-group">
                    <label class="control-label" for="selectError"><tags:label text="name"/> *</label>
                    <div class="controls">

                        <input name="name" ng-model="item.name" required/>
                    </div>
                </div>
                <div class="control-group">
                    <label class="control-label" for="selectError"><tags:label text="isactive"/> </label>
                    <div class="controls">
                        <input type="checkbox" ng-model="item.isactive" ng-checked="item.isactive" name="isactive" value="1"/>

                    </div>
                </div>
            </fields:form> 
                <div class="form-actions">
                    <a ng-click="cancel()" class="btn btn-ext-lightblue"><tags:label text="close"/></a>
                    <a ng-click="ok()" class="btn btn-ext-darkblue btn-disable-on-submit" ><tags:label text="save"/></a>
                </div>
        </form>
    </div>
</div>
</div>

在模态中,保存按钮保存修改并关闭模态。

我想在关闭后重新加载列表。我该怎么做 ? list 和 modal 的控制器不同。

模态框关闭后如何重新加载背景?

【问题讨论】:

  • 也许在你的模态关闭方法中引发一个事件,它正在关闭并在你的其他控制器中监听它?
  • 请提供更多信息,祝你好运!

标签: angularjs twitter-bootstrap angular-ui-bootstrap


【解决方案1】:

你可以广播一个方法来交流

这样试试

在触发关闭按钮的模态控制器中

$rootScope.$broadcast('updateList');

如果你想从模态传递数据

$rootScope.$broadcast('updateList',{data : 'passing'}); // pass object in {} if you wanna to pass anything

在数据控制器中

$scope.$on("updateList",function(){
   // Post your code
});

如果你从模态传递数据

$scope.$on("updateList",function(e,a){
       // Post your code
   console.log(a.data);
});

【讨论】:

  • e = 事件,a = 传递数据
【解决方案2】:

如果您使用 Angular UI $modal 服务,那么它非常简单。 $modal 服务的open() 方法在模态的closecancel 上返回一个promise。

让我们说

var myModal = $modal.open({
   animation: true,
   templateUrl: 'editForm.html',
   backdrop: 'static',
   keyboard: false,
   scope: $scope

});

myModal.result.then(function(){
    //Call function to reload the list
});

当您从列表控制器本身调用$modal.open 时,您只能在列表控制器中访问“promise”,然后您可以轻松地调用您的函数来重新加载列表。

【讨论】:

  • 我想在关闭模式后重新调用 reloadlist() 函数。我可以通过将 BrandService 注入 BrandCtrl 来做到这一点,但我不知道如何将数据从 BrandCtrl 传递给 BrandsCtrl。即使我能做到,它会刷新列表吗?
  • 如果您需要在控制器(子/父/兄弟)之间进行通信,您需要根据控制器的关系在 $scope 或 $rootScope 上使用 $broadcast / $emit。看到这个:stackoverflow.com/questions/29467339/…
  • 我尝试使用 $rootScope.$broadcast('newList', {data:list});但我无法通过 $scope.$on("newList",function(a){console.log(a.data);} 它记录未定义。
  • 你必须使用 $rootScope.$on("newList",function(a){console.log(a.data);} 。如果你使用 $rootScope 来发射/广播任何事件,那么你必须在 $rootScope 上收听
猜你喜欢
  • 2020-03-25
  • 1970-01-01
  • 2014-08-24
  • 1970-01-01
  • 2020-09-28
  • 2020-11-22
  • 2012-04-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多