Search code examples
angularjsangularjs-scopeangularjs-routingangular-ui-router

Scope inheritance fails with multiple-named views?


It breaks when moving from (Plnkr):

.state('home', {url: '/home', template: '<pre>{{parentProp}}</pre>',
                controller: function ($scope) {$scope.parentProp = ['home'];}})

To (Plnkr):

.state('home', {url: '/home', views: {'': {template: '<pre>{{parentProp}}</pre>'}},
                controller: function ($scope) {$scope.parentProp = ['home'];}})`

Specifically I have a 'sidebar' state I want to add as a secondary state to 'home'.


Solution

  • The view needs its own controller:

    .state('home', {url: '/home',
                    views: {'': {template: '<pre>{{parentProp}}</pre>',
                                 controller: function ($scope) {
                                                 $scope.parentProp = ['home'];
                                             }
                                 }
                            },
                    })`
    

    http://plnkr.co/edit/giW3XRspEV7SEPM1UDeC?p=preview