ui-router dynamic template path

前端 未结 3 1410
轻奢々
轻奢々 2020-12-01 08:01

I\'m using ui-router 0.2.8. I\'m wanting to load a template based on device width. I can get the device width without issue, set it in the scope etc but I can figure out how

3条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-01 09:07

    You can access to state params in the $stateChangeStart event. You can also dynamically update the templateUrl there as well.

    So perhaps your code might look something like this:

    angular.module('app', ['ui.router'])
    .run(function($rootScope){
        $rootScope.$on('$stateChangeStart', function(event, toState, toParams) {
            if (toState.name === 'list') {
              toState.templateUrl = 'views/'+toParams.somevalue+'/page.html';
            }
        });
    }
    

    You might also want to take a look at the onEnter callback supported by ui.router. I have not used this before but it might be neater than putting your template generating code into the $stateChangeStart event.

提交回复
热议问题