Angular ui-router to accomplish a conditional view

前端 未结 4 1276
春和景丽
春和景丽 2020-12-31 11:46

I am asking a similar question to this question: UI Router conditional ui views?, but my situation is a little more complex and I cannot seem to get the provided answer to w

4条回答
  •  傲寒
    傲寒 (楼主)
    2020-12-31 12:24

    The templateUrl can be a function as well so you check the type and return a different view and define the controller in the view rather than as part of the state configuration. You cannot inject parameters to templateUrl so you might have to use templateProvider.

    $stateProvider.state('home', {
      templateProvider: ['$stateParams', 'restService' , function ($stateParams, restService) {
        restService.getEntity($stateParams.id).then(function(entity) {
            if (entity.Type == 'first') {
                  return '
    ; } else { return '
    '; } }); }] })

提交回复
热议问题