简体   繁体   English

Angular ui-router:添加templateUrl中断路由

[英]Angular ui-router: adding templateUrl breaking routing

I am having an issue where once the templateUrl is added into the ui-router child state, the application will no longer perform the routing to the state. 我遇到一个问题,一旦将templateUrl添加到ui-router子状态,应用程序将不再执行到该状态的路由。 It works fine when it's just a template. 当它只是模板时,它可以正常工作。

app.js : app.js

app.config(['$stateProvider', '$locationProvider', '$urlMatcherFactoryProvider', '$urlRouterProvider',
    function ($stateProvider, $locationProvider, $urlMatcherFactoryProvider, $urlRouterProvider) {

        $urlMatcherFactoryProvider.caseInsensitive(true);
        $urlMatcherFactoryProvider.strictMode(false);

        $urlRouterProvider.otherwise('/page-not-found');

        $stateProvider
                .state('dashboard', {
                    url: '/',
                    views: {
                        'header': {
                            template: 'header'
                        },
                        'nav': {
                            template: 'nav'
                        },
                        main: {
                            template: 'You are on the homepage'
                        }
                    }
                });

        $locationProvider.html5Mode(true);
    }]);

app.run(['$rootScope', 'userService', '$state', function ($rootScope, user, $state) {

    $rootScope.$on("$stateChangeError", console.log.bind(console));

    if (!user.exists) {
        $state.go('user.reg');
    }
}]);

User.states.js : User.states.js

.config(['$stateProvider', function ($stateProvider) {

    $stateProvider
            .state('user', {
                url: '/users',
                abstract: true,
                views: {
                    'header': {},
                    'nav': {},
                    'main': {
                        template: '<ui-view/>'
                    }
                }
            })
            .state('user.reg', {
                url: '/register',
                //template: 'This will show fine',
                templateUrl: '/app/Users/User.login.html' // this will break
            });

}]);

UPDATE 更新

If I add a ui-sref="user.reg" to my initial pages I can navigate to the state/page fine, with the templateUrl and template . 如果我将ui-sref="user.reg"到初始页面,则可以使用templateUrltemplate导航到状态/页面正常。 So its just an issue when I try to use state.go('user.reg'); 因此,当我尝试使用state.go('user.reg');时,这只是一个问题state.go('user.reg');

This means a work around is using the $location provider to change the path. 这意味着解决方法是使用$location提供程序来更改路径。 Has the same effect but does seem rather wrong 具有相同的效果,但看起来确实是错误的

The problem is with your relative paths. 问题出在你的相对路径上。

Look at this code: 看这段代码:

$locationProvider.html5Mode(true);

You have html5 mode enabled, and for that to work, you have your base ref set in your html, which probably looks like this: 您启用了html5模式,为了使其正常工作,您在html中设置了基本引用,它可能看起来像这样:

<base href="/">

Your issue is likely that the route for your template isn't "yoursite.com/app/Users/User.login.html." 您的问题可能是模板的路由不是“ yoursite.com/app/Users/User.login.html”。

See this Plunker for a working version of your code. 有关此代码的有效版本, 请参见此Plunker Then go into the html code and uncomment out the base tag, and notice that it will break. 然后进入html代码并取消注释基本标记,并注意它将破坏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM