Search code examples
angular-ui-routernested-views

UI Router nested views for scope inheritance


I'm having trouble understanding nested views in UI Router nested views... more specifically what they look like...?

I need to understand it because I need my $scope property to be inherited.

I see this example in the docs

$stateProvider
.state('contacts', {
    abstract: true,
    url: '/contacts',
    templateUrl: 'contacts.html',
    controller: function($scope){
        $scope.contacts = [{ id:0, name: "Alice" }, { id:1, name: "Bob" }];
    }           
})
.state('contacts.list', {
    url: '/list',
    templateUrl: 'contacts.list.html'
})
.state('contacts.detail', {
    url: '/:id',
    templateUrl: 'contacts.detail.html',
    controller: function($scope, $stateParams){
      $scope.person = $scope.contacts[$stateParams.id];
    }
})

Then I see something like this in the docs

   $stateProvider
  .state('report',{
    views: {
      'filters': {
        templateUrl: 'report-filters.html',
        controller: function($scope){ ... controller stuff just for filters view ... }
      },
      'tabledata': {
        templateUrl: 'report-table.html',
        controller: function($scope){ ... controller stuff just for tabledata view ... }
      },
      'graph': {
        templateUrl: 'report-graph.html',
        controller: function($scope){ ... controller stuff just for graph view ... }
      }
    }
  })

Its all a bit confusing to me and since I need $scope inheritance I need to nest my views - just not quite sure which example that it it.

UPDATE Here are my states

$locationProvider.html5Mode(false);
    $stateProvider
        .state('search', {
        abstract: true,
        url: '/search',
        controller: 'searchCtrl',
        controllerAs: 'vm',
        templateUrl: 'search/index.html'    
    })
      .state('search.query', {
        url: '/',
        controller: 'searchCtrl',
        controllerAs: 'vm',
        templateUrl: 'search/query.html'
      })
        .state('search.results', {
        url: '/?q',//for query parameter in url
        controller: 'searchCtrl',
        controllerAs: 'vm',
        templateUrl: 'search/results.html'
      });
$urlRouterProvider.otherwise('/');

What I'm trying to achieve with this is just a simple search form on query.html, once a user enters or selects a search term then it goes to search.html with another search form and results.

These are my templates, I believe I have them set up correctly but something is wrong because nothing displays.

/app/index.html
<div ui-view></div>


/app/search/index.html
<div ui-view></div>


/app/search/query.html
<form>
...
</form>

/app/search/results.html
<div ui-view></div>
<div>
...
</div>

Solution

  • First one is an example of a nested state which fulfills your requirement for inheriting the scope object. e.g state/sub-state-a, state/sub-state-b The comment above the first snippet you took from the doc reads:

    Shows prepended url, inserted template, paired controller, and inherited $scope object.

    The second example is a nested view where you can define multiple views per state and use each depending on your use-case. From the docs:

    Then each view in views can set up its own templates (template, templateUrl, templateProvider), controllers (controller, controllerProvider).