I’ve been following the ui-router for quite some time now.
Basically it’s a state machine implementation for Angular.js, which is pretty awesome!
Today I’ve found myself wasting time trying to figure out something that should be trivial.
In a the ui-router you can define a state configuration with a “views” property which represent the ‘ui-view’ names you have in your state’s template.
I thought you should be able to define a state, and a nested views objects with it’s template & controller injections (looked like the trivial thing to do).
But when I tried it…
This showed nothing when I went to ‘/mystate’:
$stateProvider.state("myState", {
url:"/mystate",
templateUrl: mystate.tpl.html",
controller: "myStateCtrl",
views: {
'substate1': {
templateUrl: 'substate1.tpl.html',
controller: 'subState1Ctrl'
}
}
})
// mystate.tpl.html
<div>
<div ui-view="substate1"></div>
</div>
So I read and re-read the documentation trying to find a clue for whats happening, but no luck.
After some time I found an open issue describing this weird behavior, where they stated that if you define a “views” property, it automatically ignores any high level template properties you defined.
$stateProvider.state("myState", {
url:"/mystate",
templateUrl: mystate.tpl.html", -> this is being ignored
controller: "myStateCtrl",
views: {
'substate1': {
templateUrl: 'substate1.tpl.html',
controller: 'subState1Ctrl'
}
}
})
I tested it with a different variation (see below), and it worked, so I updated the documentation to save the time for the rest of us.
Different variation
I figured out that the right way to define an abstract state, and to go to it’s sub state url.
This worked when I went to ‘/mystate/substate1′ :
$stateProvider
.state("myState",
url:"/mystate",
abstract: true,
templateUrl: mystate.tpl.html",
controller: "myStateCtrl"
})
.state("myState.subState1", {
url:"/substate1",
views: {
'substate1': {
templateUrl: 'substate1.tpl.html',
controller: 'subState1Ctrl'
}
}
})
// mystate.tpl.html
<div> <div ui-view="substate1"></div> </div>
Conclusion:
When you define the ‘views’ property, be aware that the ‘templateUrl’ / ‘template’ / ‘templateProvider’ properties will be ignored, so declare an abstract state with the “Layout” and define sub states with the template & controller injections.

.png)

