使用带有Bootstrap-ui模式的ui-router

Dai*_*imz 31 angularjs bootstrap-modal angular-ui-router angular-bootstrap

我知道这已被覆盖了很多次,大多数文章都引用了这段代码:AngularJS中带有自定义URL的模态窗口

但我只是不明白.我发现根本不清楚.我也发现这个jsfiddle实际上很棒,非常有帮助,除了这不添加网址并允许我使用后退按钮关闭模态.


编辑:这是我需要帮助的.

那么让我试着解释一下我想要实现的目标.我有一个表单来添加一个新项目,我有一个链接'添加新项目'.我希望当我点击"添加新项目"时,会弹出一个模式,其中包含我创建的"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();
    };
  });
Run Code Online (Sandbox Code Playgroud)

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

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

新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>
Run Code Online (Sandbox Code Playgroud)

虽然这确实打开了一个模态,但它并没有关闭它,因为我无法解决这个问题.

Nat*_*ams 53

将模态视为状态的视图组件是直观的.使用视图模板,控制器和可能的一些结果来获取状态定义.这些特征中的每一个也适用于模态的定义.更进一步,将状态输入链接到打开模态和状态退出以关闭模式,如果你可以封装所有的管道,那么你有一个机制,可以像使用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();
                }
            }
        });
    };
})
Run Code Online (Sandbox Code Playgroud)

国家入境启动模式.州出口关闭它.模态可能会自行关闭(例如:通过背景单击),因此您必须观察并更新状态.

这种方法的好处是您的应用程序继续主要与州和州相关的概念进行交互.如果您以后决定将模态转换为传统视图,反之亦然,那么很少需要更改代码.


Daw*_*iak 7

这是一个provider通过将resolve节向下传递到controller:改进@ nathan-williams解决方案的方法:

.provider('modalState', ['$stateProvider', function($stateProvider) {
  var provider = this;

  this.$get = function() {
    return provider;
  }

  this.state = function(stateName, options) {
    var modalInstance;

    options.onEnter = onEnter;
    options.onExit = onExit;
    if (!options.resolve) options.resolve = [];

    var resolveKeys = angular.isArray(options.resolve) ? options.resolve : Object.keys(options.resolve);
    $stateProvider.state(stateName, omit(options, ['template', 'templateUrl', 'controller', 'controllerAs']));

    onEnter.$inject = ['$uibModal', '$state', '$timeout'].concat(resolveKeys);
    function onEnter($modal, $state, $timeout) {
      options.resolve = {};

      for (var i = onEnter.$inject.length - resolveKeys.length; i < onEnter.$inject.length; i++) {
        (function(key, val) {
          options.resolve[key] = function() { return val }
        })(onEnter.$inject[i], arguments[i]);
      }

      $timeout(function() { // to let populate $stateParams
        modalInstance = $modal.open(options);
        modalInstance.result.finally(function() {
          $timeout(function() { // to let populate $state.$current
            if ($state.$current.name === stateName)
              $state.go(options.parent || '^');
          });
        });
      });
    }

    function onExit() {
      if (modalInstance)
        modalInstance.close();
    }

    return provider;
  }
}]);

function omit(object, forbidenKeys) {
  var prunedObject = {};
  for (var key in object)
    if (forbidenKeys.indexOf(key) === -1)
      prunedObject[key] = object[key];
  return prunedObject;
}
Run Code Online (Sandbox Code Playgroud)

然后像这样使用它:

.config(['modalStateProvider', function(modalStateProvider) {
  modalStateProvider
    .state('...', {
      url: '...',
      templateUrl: '...',
      controller: '...',
      resolve: {
        ...
      }
    })
}]);
Run Code Online (Sandbox Code Playgroud)