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?