Angular ui-router to accomplish a conditional view

前端 未结 4 1277
春和景丽
春和景丽 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:23

    I ended up making the home controller a sibling of first and second, rather than a parent, and then had the controller of home do a $state.go to first or second depending on the results of the resolve.

    0 讨论(0)
  • 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 '<div ng-include="first.html"></div>;
            } else {
                  return '<div ng-include="second.html"></div>';
            }
        });
      }]
    })
    
    0 讨论(0)
  • 2020-12-31 12:31

    Use verified code for conditional view in ui-route

    $stateProvider.state('dashboard.home', {
            url: '/dashboard',
            controller: 'MainCtrl',
           // templateUrl: $rootScope.active_admin_template,
            templateProvider: ['$stateParams', '$templateRequest','$rootScope', function ($stateParams, templateRequest,$rootScope) {
              var templateUrl ='';
              if ($rootScope.current_user.role == 'MANAGER'){
                templateUrl ='views/manager_portal/dashboard.html';
              }else{
                templateUrl ='views/dashboard/home.html';
              }
              return templateRequest(templateUrl);
            }]
          });
    
    0 讨论(0)
  • 2020-12-31 12:36

    You can also do the following :

    $stateProvider
            .state('home', {
                url : '/{id}',
                resolve: {
                    entity: function($stateParams, RestService) {
                        return RestService.getEntity($stateParams.id);
                    }
                },
                template: 'Home Template <ui-view></ui-view>',
                onEnter: function($state, entity) {
                    if (entity.Type == 'first') {
                        $timeout(function() {
                                $state.go('home.first');
                            }, 0);
                    } else {
                        $timeout(function() {
                                $state.go('home.second');
                            }, 0);
                    }
                }
            })
            .state('home.first', {
                url: '',
                templateUrl: 'first.html',
                controller: 'FirstController'
            })
            .state('home.second', {
                url: '',
                templateUrl: 'second.html',
                controller: 'SecondController'
            });
    
    0 讨论(0)
提交回复
热议问题