1

How I can use one state for all urls? For example:

.state('home', {
    url:'/',
    template:'home.html',
    controller:'homeCtrl'
})
.state('user', {
    url:'/user',
    template:'user.html',
    controller:'userCtrl'
})
.state('master', {
    url:'*',
    controller:function() {
        console.log('hello!');
    }
})

I think in * for cath all urls, but when I try with this, nothing happend.

My idea:

When you open /user this trigger first master and after it, trigger userCtrl.

Thanks!

Olaf Erlandsen
  • 5,817
  • 9
  • 41
  • 73
  • 2
    What do you mean ? Same url for all states or a common ancestor state ? If the latter just rename the state `user` to `home.user` and define every state as 'home.[something]` see https://github.com/angular-ui/ui-router/wiki/nested-states-&-nested-views – Walfrat Dec 22 '16 at 15:26
  • 2
    Don't do it this way. Create a separate 'master' controller and have the 'homeCtrl' & 'userCtrl' extend it. You can refer to this: http://stackoverflow.com/questions/16539999/whats-the-recommended-way-to-extend-angularjs-controllers – Hoyen Dec 22 '16 at 15:28
  • I confirm, both suggestions above are good, If you have only two or three states, it's better to use the first suggestion, else, you have to choose the second suggestion – Mourad Idrissi Dec 22 '16 at 15:30
  • I have around 30 controllers, maybe extend not is a good options. What's do you think @Hoyen? – Olaf Erlandsen Dec 22 '16 at 15:32
  • @OlafErlandsen You have 30 state controllers? Because you only need to extend it for those, not every controller in your app. – Hoyen Dec 22 '16 at 15:33
  • My idea is append a "notification poll system" on `master` to show it on all states @Hoyen – Olaf Erlandsen Dec 22 '16 at 15:35
  • @OlafErlandsen Yes, then create a 'masterStateCtrl' and in all the state controllers extend it 'angular.extend(this, $controller('masterStateCtrl', {$scope: $scope}));' – Hoyen Dec 22 '16 at 15:39

0 Answers0