Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
987 views
in Technique[技术] by (71.8m points)

angularjs - Angular ui.router reload parent templateProvider

I have an abstract base template that loads the navigation based on the user type. This part works on initial loading. The problem is I can't get the parent templateProvider to reload when the user has logged in or out afterwards. I have tried the solutions here to reload the parent template, but the nav templates aren't affected. Is there a way to reload the templateProvider, or a better way of doing this? Ideally I won't have to add the nav provider to every child route.

The routes:

$stateProvider
        .state('base', {
            abstract: true,
            views: {
                content: {
                    template: '<ui-view></ui-view>',
                },
                nav: {
                    templateProvider: function ($templateFactory, User, $stateParams){
                        if(User.exists()){
                            var url = '/static/html/navs/' + User.get.type + '.html';
                            return $templateFactory.fromUrl(url);
                        }
                        else{
                            return false;
                        }
                    }
                }
            }
        })

        .state('base.index', {
            url: '/',
            controller: 'loginController',
            templateUrl: 'static/html/landing/login.html'
        })

Controller function with attempts from github issue:

scope.login_submit = function(e){
    e.preventDefault();
    User.login(scope.login, function(res){
         $state.transitionTo(
             'base.dashboard', null, 
             {reload: true, inherit: true, notify: true }
         );
    });
};
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I created working example here. It is coming from this Q & A

This would be the adjusted state def:

.state('base', { 
    abstract: true,
    views: {
        '': {
            template: '<ui-view></ui-view>',
        },
        /*
        nav: {
            templateProvider: function ($templateFactory, User, $stateParams){
                if(User.exists()){
                    var url = '/static/html/navs/' + User.get.type + '.html';
                    return $templateFactory.fromUrl(url);
                }
                else{
                    return false;
                }
            }
        },*/
        nav: {
          templateProvider: ['User', '$templateRequest', function(User, templateRequest){ 

            var tplName = 'templates/templateNotExists.html';

            if(User.exists) {

              tplName = 'templates/templateExists.html';
            }

            return templateRequest(tplName); 
          }],
        },
    }
})

As we can see, we use new feature called '$templateRequest', to get html template from server, based on the url.

and these are controllers and services

.controller('loginController', ['$scope', 'User', function ($scope, User) {
  $scope.User = User;
}])
.factory('User', function(){   return { exists: false, }; })

And this is the content of the template of the child 'base.index' state:

<input type="checkbox" ng-model="User.exists" />
<button ng-click="$state.go('base.index', null, {reload: true})" >reload</button>

Check that all in action here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...