admin管理员组

文章数量:1387303

I have a dynamic route defined as:

$urlRouterProvider
    .when(
        '/:resource?collection&type&id',
        [
            '$match', '$stateParams',
            function routeValidator( $match , $stateParams )
            {
                var path = '';
                angular.forEach($match, function joinner( val , key )
                {
                    if ( angular.isDefined(val) ) path += '/' + val;
                });
                return path;
            }
        ]
    )
    .when( '' , '/about' )
    .when( '/' , '/about' )
    .otherwise( '/404' );

And then several states:

$stateProvider
    .state('about',
        {
            "url":          "/about",
            "templateUrl":  "about.tmpl"
        }
    )
    //…

I try hitting index.html#/ or index.html#/about, and none of my states are getting invoked (and subsequently none of my controllers). BUT my routes are obeyed (ex '' gets redirected to '/about'). No console errors and values return as expected (ex. for index.html#/about, $match & path = /about).

Edit

It seems Require is part of the issue:

  • Here it's working in jsfiddle without require.
  • Here it's not working in plunker with require.

I have a dynamic route defined as:

$urlRouterProvider
    .when(
        '/:resource?collection&type&id',
        [
            '$match', '$stateParams',
            function routeValidator( $match , $stateParams )
            {
                var path = '';
                angular.forEach($match, function joinner( val , key )
                {
                    if ( angular.isDefined(val) ) path += '/' + val;
                });
                return path;
            }
        ]
    )
    .when( '' , '/about' )
    .when( '/' , '/about' )
    .otherwise( '/404' );

And then several states:

$stateProvider
    .state('about',
        {
            "url":          "/about",
            "templateUrl":  "about.tmpl"
        }
    )
    //…

I try hitting index.html#/ or index.html#/about, and none of my states are getting invoked (and subsequently none of my controllers). BUT my routes are obeyed (ex '' gets redirected to '/about'). No console errors and values return as expected (ex. for index.html#/about, $match & path = /about).

Edit

It seems Require is part of the issue:

  • Here it's working in jsfiddle without require.
  • Here it's not working in plunker with require.
Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Dec 4, 2013 at 8:38 Jakob JingleheimerJakob Jingleheimer 31.6k27 gold badges80 silver badges127 bronze badges 2
  • The first thought is how about using $locationProvider.hashPrefix('!'); and link as index.html!#/about Algularjs sometime has a mess with hashtags – IgorCh Commented Dec 4, 2013 at 9:28
  • Just tried that. Nothin' :/ Sidenote: hashPrefix('!') prepends to the route after the # symbol: index.html#!/about – Jakob Jingleheimer Commented Dec 4, 2013 at 19:27
Add a ment  | 

1 Answer 1

Reset to default 9

Turn's out I'm an idiot: When I switched from ngRouter to ui.router, I forgot to switch ng-view to ui-view. Working plunkr: http://plnkr.co/edit/ZckIBlayuB10hooJ0sY5

本文标签: javascriptangularui uirouter dynamic routes and statesStack Overflow