【问题标题】:Closing Modal Dialog does not return to original html page关闭模态对话框不返回原始 html 页面
【发布时间】:2016-12-19 20:02:53
【问题描述】:

我有一个从主标题栏中显示的非常简单的模态对话框。 当我关闭模式对话框时,它不会返回到原始页面。地址栏中显示的 url 是我关闭的 modal。 如何返回单击按钮时显示的原始页面。 这是我的代码: 这是app.js 中的模态,reportSettingModal 的显示方式。

.config(function ($stateProvider, $urlRouterProvider) {
    $stateProvider
      .state('login', {
        url: '/login',
        templateUrl: 'lib/views/login.html',
        controller: 'LoginCtrl'
      })
      .state('home', {
        url: '/home',
        templateUrl: 'views/home.html',
        controller: 'HomeCtrl'
      })
      .state('renderReport',{
        url: '/renderReport/{guid}',
        templateUrl: 'views/renderReport.html',
        controller: 'RenderReportCtrl'
      })

      .state('vAnalyze',{
        url: '/vAnalyze',
        templateUrl: 'views/vAnalyze.html',
        controller: 'VAnalyzeCtrl'
      })
       .state('reportSetting',{
        url: '/reportSettingModal',
        onEnter: function($modal){
          $modal.open({
              templateUrl: 'views/reportSettingModal.html',
               controller: 'ReportSettingModalCtrl'
          });
        }
      });

这是 HTML:

<div class="modal-header" id="reportSettingModal" style="background-color: #A33441; color: white;">
    <h4 class="modal-title">{{title}}</h4>
</div>
<div class="modal-body">
    <button type="button" class="btn btn-danger" style="margin-right: 5px; margin-left: 5px;" ng-click="updateWarehouse()">Update Warehouse</button>
</div>  
<div class="modal-footer">
    <button class="btn btn-sm btn-vow" id="closeButton" ng-click="close()">Close</button>
  </div>

这是控制器:

angular.module('vAnalyzeApp.controllers')
.controller('ReportSettingModalCtrl', [
    '$scope',
    '$uibModal',
    'Session',
     '$uibModalInstance',
    function($scope, $uibModal, session,  $uibModalInstance){
            $scope.username = session.user;
            $scope.title = 'Report Settings';

    $scope.close = function() {
      $uibModalInstance.close();
    };
  } //end main function
]);

所以,如果我在 VAnalyze 页面上并打开模式,当我关闭模式时,我想在 VAnalyze 页面上。

更新

.run(['AuthService', 'configSettings', '$rootScope',
    function (AuthService, configSettings, $rootScope) {

      // Apply Product Code
      configSettings.productCode = 'VANALYZE';
      AuthService.configProduct(configSettings.productCode);
    },
    function ($rootScope) {
       $rootScope.$on('$stateChangeSuccess', function(event, to, toParams, from, fromParams) {
        $rootScope.$previousState = from;
      });
    }

  ]);

更新 我在模态关闭函数中添加了state.go 函数,但在打开模态时仍然没有返回上一个 url。这是我目前的关闭功能:

$scope.close = function() {
      $uibModalInstance.close();
      $state.go($rootScope.$previousState.name, {
        url: $rootScope.$previousState.url,
        templateUrl: $rootScope.$previousState.templateUrl,
        controller: $rootScope.$previousState.controller
      });
    };

通过代码,url、name templateUrl 和控制器都已正确设置。

【问题讨论】:

    标签: angularjs modal-dialog


    【解决方案1】:

    选项 1:

    在您的关闭函数中,您应该指定要进入的状态。一旦用户关闭模式。

    $scope.close = function() {
       $uibModalInstance.close();
       $state.go('state-name');
    };
    

    选项 2:

    如果 URL 不相关,可以从 VAnalyze 状态控制器调用 Modal 服务,即 VAnalyzeCtrl:

    $modal.open({
        templateUrl: 'views/reportSettingModal.html',
        controller: 'ReportSettingModalCtrl'
    });
    

    通过这样做,您不必将用户重定向到以前的 URL。

    【讨论】:

      【解决方案2】:

      要返回之前的状态,您需要存储它,以便在模式关闭时重新路由到它。

      你需要在你的 app.js 文件中添加这个函数,就像你定义config一样,

      .run(['AuthService', 'configSettings', '$rootScope', function (AuthService, configSettings, $rootScope) 
        { // Apply Product Code 
          configSettings.productCode = 'VANALYZE';
          AuthService.configProduct(configSettings.productCode); 
          $rootScope.$on('$stateChangeSuccess', function(event, to, toParams, from,      fromParams) {
              $rootScope.$previousState = from;
          });
        } ]);
      

      在您的ReportSettingModalCtrl 中,

      angular.module('vAnalyzeApp.controllers')
      .controller('ReportSettingModalCtrl', [
      '$scope',
      '$uibModal',
      'Session',
       '$uibModalInstance',
       '$state',
       '$rootScope' 
      function($scope, $uibModal, session,  $uibModalInstance, $state, $rootScope){
              $scope.username = session.user;
              $scope.title = 'Report Settings';
      
      $scope.close = function() {
        $uibModalInstance.close();
        $state.go($rootScope.$previousState.name);
         };
        } //end main function
      ]);
      

      【讨论】:

      • 更新答案。如果它解决了您的问题,请接受答案。
      • 我在文件中添加了$rootScope 函数时遇到了一点麻烦。已经有一个运行功能。我将它添加到原始帖子中。我尝试添加您的功能。它编译但不显示登录页面。
      • 您要记录什么以及在哪里记录?
      • 当应用程序最初运行时,会有一个登录屏幕。当我像上面那样更新.run 函数并启动应用程序时,它只是一个空白页面,而不是登录页面。所以,我错误地更新了.run 函数。这个run方法里能有2个函数吗?
      • 不,我不这么认为。按照我的回答,尝试将这两个功能合二为一。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-25
      • 1970-01-01
      • 1970-01-01
      • 2013-02-11
      相关资源
      最近更新 更多