最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Angular ui routers nested ui-views not working - Stack Overflow

programmeradmin1浏览0评论

I'm trying to create a simple nested route within angular and whenever a nested route occurs the view doesn't pop up

With a path of http://localhost:9000/#/home/hello I still only see homeHello

Why isn't the nested ui view picking up the home.hello template?

Rendered HTML

<section ui-view="" class="ng-scope">
  <section class="home ng-scope">
    home
    <a ui-sref=".hello" href="#/home/hello">Hello</a>
    <!-- uiView: ui-view -->
    <div ui-view="ui-view" class="ng-scope"></div>
  </section>
</section>

Angular Ui Router

// app.js

angular.module('spoonFeeder',
  ['ui.router',
   'ngAnimate',
   'ngCookies',
   'ngResource',
   'ngSanitize',
   'ngTouch'])

.config(function($stateProvider, $urlRouterProvider, $locationProvider) {

    $urlRouterProvider.otherwise('/home')

    $stateProvider
        .state('home', {
            url: '/home',
            templateUrl: 'home/home.html'
        })
        .state('home.hello', {
            url: '/hello',
            templateUrl: 'home/hello.html'
        })

        // use the HTML5 History API
        // $locationProvider.html5Mode(true);
});

Views

<!-- home/home`.html -->
<section class="home">home<a ui-sref=".hello">Hello</a>
  <div ui-view="ui-view"></div>
</section>

<!-- home/hello.html -->
<section class="hello">Hello</section>

I'm trying to create a simple nested route within angular and whenever a nested route occurs the view doesn't pop up

With a path of http://localhost:9000/#/home/hello I still only see homeHello

Why isn't the nested ui view picking up the home.hello template?

Rendered HTML

<section ui-view="" class="ng-scope">
  <section class="home ng-scope">
    home
    <a ui-sref=".hello" href="#/home/hello">Hello</a>
    <!-- uiView: ui-view -->
    <div ui-view="ui-view" class="ng-scope"></div>
  </section>
</section>

Angular Ui Router

// app.js

angular.module('spoonFeeder',
  ['ui.router',
   'ngAnimate',
   'ngCookies',
   'ngResource',
   'ngSanitize',
   'ngTouch'])

.config(function($stateProvider, $urlRouterProvider, $locationProvider) {

    $urlRouterProvider.otherwise('/home')

    $stateProvider
        .state('home', {
            url: '/home',
            templateUrl: 'home/home.html'
        })
        .state('home.hello', {
            url: '/hello',
            templateUrl: 'home/hello.html'
        })

        // use the HTML5 History API
        // $locationProvider.html5Mode(true);
});

Views

<!-- home/home`.html -->
<section class="home">home<a ui-sref=".hello">Hello</a>
  <div ui-view="ui-view"></div>
</section>

<!-- home/hello.html -->
<section class="hello">Hello</section>
Share Improve this question edited Mar 11, 2016 at 10:30 Hernan 1,1481 gold badge12 silver badges30 bronze badges asked Jul 21, 2014 at 22:01 MikaAKMikaAK 2,3646 gold badges27 silver badges56 bronze badges 2
  • 2 Your ui-sref on your child state should include the parent. e.g. ui-sref="home.hello" AFAIK – Christopher Marshall Commented Jul 21, 2014 at 22:04
  • It doesn't need to this is relative pathing. even with it on there it doesn't work – MikaAK Commented Jul 21, 2014 at 22:21
Add a ment  | 

1 Answer 1

Reset to default 4

There is a plunker with a working example. What I changed was: "extending of the parent template"

<section class="home">home
  <a ui-sref=".hello">Hello</a>
  <div ui-view="ui-view"></div>
  <div ui-view=""></div>      <!-- new line -->
</section>

The new element div does contain also attribute ui-view, but in this case, it is unnamed so this state definition can find it:

.state('home.hello', {
  url: '/hello',
  templateUrl: 'tpl.hello.html',
})

To show, how we can target the first one ui-view="ui-view", which is in fact named, there is new state Hello2:

.state('home.hello2', {
  url: '/hello2',
  views : {
    'ui-view' : {
      templateUrl: 'tpl.hello2.html',
    }
  }
})

and this state, is now really targeting the named ui-view="ui-view, because it uses explicit views {} defintion. The state hello, on the other hand uses implicit views definition which could be expressed like this:

  views : {
    '' : { // targeting unnamed ui-view=""
      templateUrl: 'tpl.hello2.html',
    }
  }

Check it here

发布评论

评论列表(0)

  1. 暂无评论