使用angular-ui-bootstrap在AngularJS中的路径中打开模态

时间:2014-02-19 14:33:45

标签: javascript angularjs angular-ui-bootstrap angular-ui-router

我正在努力做到这里基本上回答的问题Unable to open bootstrap modal window as a route

然而,我的解决方案是行不通的。我收到错误

Error: [$injector:unpr] Unknown provider: $modalProvider <- $modal

My app has the ui.bootstrap module injected - here is my application config

var app = angular.module('app', ['ui.router', 'ui.bootstrap','ui.bootstrap.tpls', 'app.filters', 'app.services', 'app.directives', 'app.controllers'])

    // Gets executed during the provider registrations and configuration phase. Only providers and constants can be
    // injected here. This is to prevent accidental instantiation of services before they have been fully configured.
    .config(['$stateProvider', '$locationProvider', function ($stateProvider, $locationProvider) {

        // UI States, URL Routing & Mapping. For more info see: https://github.com/angular-ui/ui-router
        // ------------------------------------------------------------------------------------------------------------   

        $stateProvider
            .state('home', {
                url: '/',
                templateUrl: '/views/index',
                controller: 'HomeCtrl'

            })
            .state('transactions', {
                url: '/transactions',
                templateUrl: '/views/transactions',
                controller: 'TransactionsCtrl'
            })      
            .state('login', {
                url: "/login",
                templateUrl: '/views/login',
                controller: 'LoginCtrl'                
            })

            .state('otherwise', {
                url: '*path',
                templateUrl: '/views/404',
                controller: 'Error404Ctrl'
            });

        $locationProvider.html5Mode(true);

    }])

我已将控制器缩减为以下内容:

appControllers.controller('LoginCtrl', ['$scope', '$modal', function($scope, $modal) {
    $modal.open({templateUrl:'modal.html'});
}]);

最终,我希望实现的目标是,当需要登录时,实际上并未进入登录页面,而是启动对话框。

我也尝试在onEnter状态方法中使用ui-router函数。无法使这个工作。

有什么想法吗?

更新

好的 - 事实证明,同时使用ui-bootstrap.js和ui-bootstrap-tpls会破坏这一点 - 在阅读文档之后,我认为你需要使用模板来使用ui-bootstrap。虽然似乎所有的掠夺者只在.tpls文件中加载 - 一旦我删除了ui-bootstrap文件我的模态工作......我瞎了吗?或者它不是真的在github上的文档中说出你需要哪一个? -

现在我只需要弄明白如何防止我的网址真正进入/登录,而不是仅仅显示模态:)

更新2

好的,所以通过在服务中调用$ state.go('login')来为我做这件事。

4 个答案:

答案 0 :(得分:11)

您好我遇到了类似的问题。 但是,我能够解决它。 这就是你可能需要的。

app.config(function($stateProvider) {
  $stateProvider.state("managerState", {
      url: "/ManagerRecord",
      controller: "myController",
      templateUrl: 'index.html'
    })
    .state("employeeState", {
      url: "empRecords",
      parent: "managerState",
      params: {
        empId: 0
      },
      onEnter: [
        "$modal",
        function($modal) {
          $modal.open({
            controller: "EmpDetailsController",
            controllerAs: "empDetails",
            templateUrl: 'empDetails.html',
            size: 'sm'
          }).result.finally(function() {
            $stateProvider.go('^');
          });
        }
      ]
    });
});

Click here对于plunker。希望它有所帮助。

答案 1 :(得分:5)

我正在做类似的事情,这是我的解决方案。

HTML代码

<a ui-sref="home.modal({path: 'login'})" class="btn btn-default" ng-click="openModal()">Login</a>

州配置

$stateProvider
  // assuming we want to open the modal on home page
  .state('home', {
    url: '/',
    templateUrl: '/views/index',
    controller: 'HomeCtrl'
  })
  // create a nested state
  .state('home.modal', {
    url: ':path/'
  });

家庭控制器

//... other code
$scope.openModal = function(){
  $modal.open({
    templateUrl: 'path/to/page.html',
    resolve: {
            newPath: function(){
                return 'home'
            },
            oldPath: function(){
                return 'home.modal'
            }
        },
    controller: 'ModalInstanceController'
  });
};
//... other code

最后,模态实例控制器。 此控制器将模态事件(打开/关闭)与URL路径更改同步。

angular.module("app").controller('ModalInstanceController', function($scope,    $modalInstance, $state, newPath, oldPath) {
    $modalInstance.opened.then(function(){
        $state.go(newPath);
    });
    $modalInstance.result.then(null,function(){
        $state.go(oldPath);
    });
    $scope.$on('$stateChangeSuccess', function () {
        if($state.current.name != newPath){
            $modalInstance.dismiss('cancel')
        }
    });
});

答案 2 :(得分:3)

您可以创建一个与您要显示模式的页面具有相同templateUrlcontroller的州,并向其添加params个对象

$stateProvider
    .state('root.start-page', {
        url: '/',
        templateUrl: 'App/src/pages/start-page/start-page.html',
        controller: 'StartPageCtrl'
    })
    .state('root.login', {
        url: '/login',
        templateUrl: 'App/src/pages/start-page/start-page.html',
        controller: 'StartPageCtrl',
        params: {
            openLoginModal: true
        }
    })

在页面控制器中,使用此参数打开模态

.controller("StartPageCtrl", function($scope, $stateParams) {
    if ($stateParams.openLoginModal) {
        $scope.openLoginModal();
    }

答案 3 :(得分:2)

我找到了一个方便的提示让这个工作。可能有一些警告,但它对我有用。您可以传递result但我不需要。

使用finally代替then承诺解析为我排序。我还必须将以前的状态存储在rootScope上,以便我们知道要返回的内容。

将以前的状态保存到$ rootScope

$rootScope.previousState = 'home';
$rootScope.$on('$stateChangeSuccess', function(ev, to, toParams, from, fromParams){
    $rootScope.previousState = from.name;
})

使用onEnter状态

$stateProvider.state('contact', {
    url: '/contact',
    onEnter: function ($state, $modal, $rootScope){
        $modal.open({
            templateUrl: 'views/contact.html',
            controller: 'ContactCtrl'
        }).result.finally(function(){
            $state.go($rootScope.previousState);
        })
    }
});