0

So I have a directive that accepts a template name which dynamically loads the correct template. It looks like this:

angular.module('widget.directives').directive('pkSplash', directive);

function directive() {
    return {
        restrict: 'A',
        controller: 'PkSplashController',
        controllerAs: 'controller',
        bindToController: true,
        template: '<div ng-include="contentUrl"></div>',
        link: lnkFn
    };

    function lnkFn(scope, element, attrs, controller) {
        scope.contentUrl = 'app/directives/pkSplash-' + attrs.pkSplash + '.html';
        attrs.$observe('template', function (template) {
            scope.contentUrl = 'app/directives/pkSplash-' + template + '.html';
        });
        scope.$watch(controller.loading, function (loading) {
            controller.loaded = !loading;
        });
    };
};

The directive actually sits on the index.html page like this:

<div pk-splash="{{ loaderTemplate }}" ng-if="loaderTemplate"></div>

and the loaderTemplate is set on the $rootScope when a state change starts, like this:

function run($rootScope, pkSplashService) {
    $rootScope.$on('$stateChangeStart', function (event, toState) {
        $rootScope.loaderTemplate = pkSplashService.getTemplate(toState.name);
        console.log($rootScope.loaderTemplate);
    });
};

I have put console logs on the $stateChangeStart method and I can see that when swapping states that the template name does change, but the loader will only ever use the template that is first loaded. Does anyone know how I can get it to change?

1 Answer 1

1

In this part it looks like you are observing an attribute called 'template' but you aren't passing one:

    attrs.$observe('template', function (template) {
        scope.contentUrl = 'app/directives/pkSplash-' + template + '.html';
    });

In that case, you would need to use it like this:

<div pk-splash template="{{ loaderTemplate }}" ng-if="loaderTemplate"></div>

The other option would be to observe the pkSplash attribute instead:

 attrs.$observe('pkSplash', function (template) {
     scope.contentUrl = 'app/directives/pkSplash-' + template + '.html';
 });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.