小编典典

在Bootstrap-ui模式下使用ui-router

angularjs

我知道它已经被讨论过很多次了,大多数文章都引用了以下代码:AngularJS中带有自定义URL的模态窗口

但是我就是不明白。我根本看不出来。我还发现这个jsfiddle实际上很棒,非常有帮助,除了它不添加url并允许我使用后退按钮关闭模式。


编辑:这是我需要帮助。

因此,让我尝试解释我要实现的目标。 我有一个添加新项目的表格,并且有一个“添加新项目”链接。我想当我单击“添加新项目”时,使用我创建的“ add-
item.html”形式弹出模式。这是一个新状态,因此url更改为/add-item。我可以填写表格,然后选择保存或关闭。关闭,关闭模式:p(多么奇怪)。但是我也可以单击返回以关闭模式,并返回到上一页(状态)。

由于我仍在努力使模态正常工作,因此我此时不需要Close方面的帮助。


这是我目前的代码:

导航控制器:( 这是否是放置模态函数的正确位置?)

angular.module('cbuiRouterApp')
  .controller('NavbarCtrl', function ($scope, $location, Auth, $modal) {
    $scope.menu = [{
      'title': 'Home',
      'link': '/'
    }];

    $scope.open = function(){

        // open modal whithout changing url
        $modal.open({
          templateUrl: 'components/new-item/new-item.html'
        });

        // I need to open popup via $state.go or something like this
        $scope.close = function(result){
          $modal.close(result);
        };
      };

    $scope.isCollapsed = true;
    $scope.isLoggedIn = Auth.isLoggedIn;
    $scope.isAdmin = Auth.isAdmin;
    $scope.getCurrentUser = Auth.getCurrentUser;

    $scope.logout = function() {
      Auth.logout();
      $location.path('/login');
    };

    $scope.isActive = function(route) {
      return route === $location.path();
    };
  });

这就是我激活模式的方式:

 <li ng-show='isLoggedIn()' ng-class='{active: isActive("/new-item")}'>
   <a href='javascript: void 0;' ng-click='open()'>New Item</a>
 </li>

new-item.html:

<div class="modal-header">
  <h3 class="modal-title">I'm a modal!</h3>
</div>
<div class="modal-body">
  <ul>
    <li ng-repeat="item in items"><a ng-click="selected.item = item">{{ item }}</a></li>
  </ul>Selected:<b>{{ selected.item }}</b>
</div>
<div class="modal-footer">
  <button ng-click="ok()" class="btn btn-primary">OK</button>
  <button ng-click="close()" class="btn btn-primary">OK</button>
</div>

同样,尽管这确实打开了一个模式,但由于我无法解决,所以无法关闭它。


阅读 337

收藏
2020-07-04

共1个答案

小编典典

将模态视为状态的视图组件很直观。用视图模板,控制器和一些解决方案来进行状态定义。这些功能中的每一个也适用于模态的定义。再往前走,将状态输入链接到打开模式,将状态出口链接到关闭模式,如果可以封装所有管道,则可以使用一种机制,就像带有状态ui- sref$state.go用于输入的状态以及返回按钮一样。或更多特定于模式的退出触发器。

我已经对此进行了广泛的研究,我的方法是创建一个模态状态提供程序,该模版状态提供程序可类似于$stateProvider配置模块以定义绑定到模态的状态时使用。当时,我特别感兴趣的是通过状态和模态事件统一控制模态解雇,这比您要的要复杂得多,因此这里是一个简化的示例

关键是使模态成为状态的责任,并使用模态提供的钩子使状态与模态通过作用域或其UI支持的独立交互保持同步。

.provider('modalState', function($stateProvider) {
    var provider = this;
    this.$get = function() {
        return provider;
    }
    this.state = function(stateName, options) {
        var modalInstance;
        $stateProvider.state(stateName, {
            url: options.url,
            onEnter: function($modal, $state) {
                modalInstance = $modal.open(options);
                modalInstance.result['finally'](function() {
                    modalInstance = null;
                    if ($state.$current.name === stateName) {
                        $state.go('^');
                    }
                });
            },
            onExit: function() {
                if (modalInstance) {
                    modalInstance.close();
                }
            }
        });
    };
})

状态输入启动模式。状态出口将其关闭。模态可能会自行关闭(例如,通过背景幕单击),因此您必须观察并更新状态。

这种方法的好处是您的应用继续主要与状态和与状态相关的概念进行交互。如果您以后决定将模式转换为常规视图,反之亦然,则只需更改很少的代码。

2020-07-04