使用 angular-ui-bootstrap 在 AngularJS 的路由中打开一个模态 [英] opening a modal in a route in AngularJS with angular-ui-bootstrap

查看:20
本文介绍了使用 angular-ui-bootstrap 在 AngularJS 的路由中打开一个模态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试做这里基本上回答的事情无法打开引导模式窗口作为路由

I am trying to do what was essentially answered here Unable to open bootstrap modal window as a route

然而我的解决方案是行不通的.我收到一个错误

Yet my solution just will not work. I get an error

错误:[$injector:unpr] 未知提供者:$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);

    }])

我已将控制器简化为以下内容:

I have reduced my controller to the following:

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

最终,我希望实现的是在需要登录时实际上不是转到登录页面,而是弹出一个对话框.

我也尝试过在 ui-router 状态方法中使用 onEnter 函数.也无法正常工作.

I have also tried using the onEnter function in the ui-router state method. Couldn't get this working either.

有什么想法吗?

更新

好的 - 事实证明,同时拥有 ui-bootstrap.js 和 ui-bootstrap-tpls 打破了这一点 - 在阅读文档后,我认为你需要模板与 ui-bootstrap 一起工作.尽管似乎所有的 plunkers 都只加载到 ..tpls 文件中 - 一旦我删除了 ui-bootstrap 文件,我的模式就可以工作了……我是瞎了吗?或者它不是真的在 github 上的文档中说明您需要哪一个?-

Ok - so as it turns out, having both ui-bootstrap.js AND ui-bootstrap-tpls breaks this - After reading the docs I thought you needed the templates to work WITH the ui-bootstrap. though it seems all the plunkers only load in the ..tpls file - once I removed the ui-bootstrap file my modal works...Am i blind? or doesn't it not really say which one you need in the docs on github? -

现在我只需要弄清楚如何防止我的 url 实际上转到/login,而不仅仅是显示模态 :)

Now i just need to figure out how to prevent my url from actually going to /login, rather than just show the modal :)

更新 2

好的,所以通过在服务中调用 $state.go('login') 可以帮我做到这一点.

Ok, so by calling $state.go('login') in a service does this for me.

推荐答案

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

Hi I had a hard time getting through the similar problem. However, I was able to resolve it. This is what you would probably need.

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('^');
          });
        }
      ]
    });
});

点击此处查看 plunker.希望有帮助.

Click here for plunker. Hope it helps.

这篇关于使用 angular-ui-bootstrap 在 AngularJS 的路由中打开一个模态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆