1. <legend id='KNKVL'><style id='KNKVL'><dir id='KNKVL'><q id='KNKVL'></q></dir></style></legend>
    <i id='KNKVL'><tr id='KNKVL'><dt id='KNKVL'><q id='KNKVL'><span id='KNKVL'><b id='KNKVL'><form id='KNKVL'><ins id='KNKVL'></ins><ul id='KNKVL'></ul><sub id='KNKVL'></sub></form><legend id='KNKVL'></legend><bdo id='KNKVL'><pre id='KNKVL'><center id='KNKVL'></center></pre></bdo></b><th id='KNKVL'></th></span></q></dt></tr></i><div id='KNKVL'><tfoot id='KNKVL'></tfoot><dl id='KNKVL'><fieldset id='KNKVL'></fieldset></dl></div>
  2. <tfoot id='KNKVL'></tfoot>
  3. <small id='KNKVL'></small><noframes id='KNKVL'>

        <bdo id='KNKVL'></bdo><ul id='KNKVL'></ul>

      Angular UI 路由器 - 使用 ui-sref 导航到动态状态时会出现双斜杠

      Angular UI Router - Dynamic states get double slashes when navigated to with ui-sref(Angular UI 路由器 - 使用 ui-sref 导航到动态状态时会出现双斜杠)

      1. <legend id='98ABd'><style id='98ABd'><dir id='98ABd'><q id='98ABd'></q></dir></style></legend>

            1. <small id='98ABd'></small><noframes id='98ABd'>

                <tbody id='98ABd'></tbody>
              <i id='98ABd'><tr id='98ABd'><dt id='98ABd'><q id='98ABd'><span id='98ABd'><b id='98ABd'><form id='98ABd'><ins id='98ABd'></ins><ul id='98ABd'></ul><sub id='98ABd'></sub></form><legend id='98ABd'></legend><bdo id='98ABd'><pre id='98ABd'><center id='98ABd'></center></pre></bdo></b><th id='98ABd'></th></span></q></dt></tr></i><div id='98ABd'><tfoot id='98ABd'></tfoot><dl id='98ABd'><fieldset id='98ABd'></fieldset></dl></div>

              • <bdo id='98ABd'></bdo><ul id='98ABd'></ul>
                <tfoot id='98ABd'></tfoot>
                本文介绍了Angular UI 路由器 - 使用 ui-sref 导航到动态状态时会出现双斜杠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在创建一个 CMS 系统,因此我希望动态创建状态.由于您无法在配置阶段发出 http 请求,我决定在 .run() 函数中添加我的路由,如下所述:http://blog.brunoscopelliti.com/how-to-defer-route-definition-in-an-angularjs-web-app

                I'm creating a CMS system so I want the states to be dynamically created. Since you can't make http requests within the config phase I decided to add my routes in the .run() function as described here: http://blog.brunoscopelliti.com/how-to-defer-route-definition-in-an-angularjs-web-app

                现在,当我使用 ui-sref 指令在导航部分中显示链接列表时,附加的 url 有 2 个正斜杠而不是一个.控制器和一切都被相应地调用,但我似乎无法摆脱双斜线.有什么想法吗?

                Now when I display a list of links in my navigation partial using the ui-sref directive the url that is appended has 2 forward slashes instead of one. The controller and everything is called accordingly but I can't seem to get rid of the double slashes. Any ideas?

                以下是相关代码:

                app.js

                var $stateProviderReference;
                
                angular.module('app', [
                    'ui.router',                // Angular UI Routing
                    'ngStorage',                // Angular Storage (local, cookies, session)
                    'ngAnimate',                // Angular Animation
                    'angular-loading-bar',      // Angular Loading Bar
                    'restangular',              // Restangular
                    'angular.assets.injector',  // Custom assets injector
                ])
                .config(function($stateProvider, $urlRouterProvider, RestangularProvider, $httpProvider, cfpLoadingBarProvider){
                
                    // Save a reference of our stateProvider
                    $stateProviderReference = $stateProvider;
                
                    // Irrelevant configuration
                
                    // States (routes)
                    $stateProvider
                
                        /*
                         |
                         | Frontend
                         |
                         */
                        .state('frontend', {
                            url: '/',
                            views: {
                                '': { 
                                    templateUrl: 'app/views/frontend/templates/default/bootstrap.html',
                                    controller: 'FrontendCtrl'
                                },
                                'nav@frontend': {
                                    templateUrl: 'app/views/frontend/templates/default/partials/navigation.html',
                                    controller: 'NavCtrl',
                                    resolve: {
                                        pages: function(PageService){
                                            return PageService.all();
                                        }
                                    }
                                },
                                'footer@frontend': {
                                    templateUrl: 'app/views/frontend/templates/default/partials/footer.html',
                                    controller: 'FooterCtrl'
                                },
                                'page@frontend': {
                                    templateUrl: 'app/views/frontend/pages/page.html',
                                    controller: 'PageCtrl'
                                }
                            }
                        });
                
                .run(function($rootScope, $location, $state, $stateParams, AuthService, CSRF_TOKEN, PageService){
                
                    // Retrieve all the pages
                    PageService.all().then(function(pages){
                
                    // Loop through all the pages
                    angular.forEach(pages, function(page){
                
                        // If this is not the homepage
                        if( page.routeName !== 'frontend' )
                        {
                            // Add a state to our stateProvider
                            $stateProviderReference.state(page.routeName, {
                                // Set the desired url
                                url: page.url,
                                // Views that we are populating
                                views: {
                                    // Override the page view partial
                                    'page@frontend': {
                                        templateUrl: 'app/views/frontend/pages/page.html',
                                        controller: 'PageCtrl'
                                    }
                                }
                            });
                        }
                
                    });
                
                });
                

                navigation.html 部分

                navigation.html partial

                <ul class="nav navbar-nav">
                    <li ng-repeat="page in pages">
                        <a ng-class="{'active': isActive({{page.routeName}})}" ui-sref="{{ page.routeName }}">{{ page.title }}</a>
                    </li>
                </ul>
                

                从数据库中检索的页面数据格式如下:

                Page data being retrieved from the db is formatted like this:

                {
                    id: 1,
                    routeName 'frontend.home',
                    title: 'Homepage',
                    url: '/home',
                    metaData: {
                        tags: "some random tags",
                        desc: "some random description"
                    },
                    content: "Html goes here",
                    deletable: 'yes' // or 'no'
                }
                

                所以除了双斜杠之外,一切似乎都正常.当我点击为上述示例代码生成的链接时,它会将我发送到以下网址:

                So everything seems to work except for the double slashes. When I click on the link generated for the above sample code it will send me to the following url:

                http://localhost/CMSv2/public/#//home
                

                而不是

                http://localhost/CMSv2/public/#/home
                

                我尝试从 routeName 属性中删除正斜杠,然后它没有附加双斜杠,但无法再访问控制器,一切似乎都坏了.

                I have tried removing the forward slash from the routeName property and then it does not append the double slashes but the controller can't be reached anymore and everything seems to break.

                推荐答案

                问题出在这样一个事实,如果有父子状态,他们的url就是parent.url + child.url:

                The issue comes from the fact, that if there are states parent - child their url is parent.url + child.url:

                • 父级:.state('frontend', { url: '/',...
                • child: { routeName 'frontend.home', url: '/home',..
                • 结果:'/' + '/home' === '//home'

                ui-router 中内置了一个不错的解决方案:

                But again there is a nice solution built into the ui-router:

                • 绝对路由 (^)

                小引:

                如果你想进行绝对 url 匹配,那么你需要在你的 url 字符串前加上一个特殊符号 '^'.

                If you want to have absolute url matching, then you need to prefix your url string with a special symbol '^'.

                $stateProvider
                  .state('contacts', {
                     url: '/contacts',
                     ...
                  })
                  .state('contacts.list', {
                     url: '^/list',
                     ...
                  });
                

                解决方案: (在这种情况下非常简单)

                而不是这个url def:

                Instead of this url def:

                {
                    id: 1,
                    routeName 'frontend.home',
                    title: 'Homepage',
                    url: '/home',
                    ...
                

                我们只是将路径这个 url: '^/home',:

                We just will path this url: '^/home',:

                {
                    id: 1,
                    routeName 'frontend.home',
                    title: 'Homepage',
                    url: '^/home',
                    ...
                

                这篇关于Angular UI 路由器 - 使用 ui-sref 导航到动态状态时会出现双斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Toggle HTML radio button by clicking its label(通过单击标签来切换 HTML 单选按钮)
                Javascript how to change radio button label text?(Javascript如何更改单选按钮标签文本?)
                JavaScript radio button confirmation(JavaScript 单选按钮确认)
                How can I automatically select specific radio buttons with Greasemonkey?(如何使用 Greasemonkey 自动选择特定的单选按钮?)
                AngularJs. Is it possible to deselect HTML “radio” input by click?(AngularJs.是否可以通过单击取消选择 HTML“收音机输入?)
                Checking Value of Radio Button Group via JavaScript?(通过 JavaScript 检查单选按钮组的值?)

                    <tfoot id='lfiqF'></tfoot>
                    • <small id='lfiqF'></small><noframes id='lfiqF'>

                        <tbody id='lfiqF'></tbody>
                    • <i id='lfiqF'><tr id='lfiqF'><dt id='lfiqF'><q id='lfiqF'><span id='lfiqF'><b id='lfiqF'><form id='lfiqF'><ins id='lfiqF'></ins><ul id='lfiqF'></ul><sub id='lfiqF'></sub></form><legend id='lfiqF'></legend><bdo id='lfiqF'><pre id='lfiqF'><center id='lfiqF'></center></pre></bdo></b><th id='lfiqF'></th></span></q></dt></tr></i><div id='lfiqF'><tfoot id='lfiqF'></tfoot><dl id='lfiqF'><fieldset id='lfiqF'></fieldset></dl></div>
                        <bdo id='lfiqF'></bdo><ul id='lfiqF'></ul>

                        1. <legend id='lfiqF'><style id='lfiqF'><dir id='lfiqF'><q id='lfiqF'></q></dir></style></legend>