0

So right now I have a parent directive for an entire view so I can swap out templates based on the return status of a promise.

.directive('myDirective', function(myService) {
    var rootDir = '/path/to/templates';

    return {
         restrict: 'E',
         replace: true,
         controller: controller,
         link: linker,
         template: '<div ng-include="getContentUrl()"></div>'
    };

    function controller($scope) { ... }

    function linker(scope) { 
        myService.getData().then(function(res) {
            scope.getContentUrl = function() {
                var tpl = res.status >= 400 ? '/tplContent.html' : '/tplError.html';
                return rootDir + tpl; 
            };
        });    
    }
})

However, this would be much cleaner if I could call myService.getData() while resolving my angular-ui route and dynamically load the templateUrl after my promise resolves.

1 Answer 1

1

What if you assign the results of getData() to the scope, and bind to that?

template: '<div ng-include="{{contentUrl}}"></div>'

function linker(scope) { 
    myService.getData().then(function(res) {
        var getContentUrl = function() {
            var tpl = res.status >= 400 ? '/tplContent.html' : '/tplError.html';
            return rootDir + tpl; 
        };

        $scope.contentUrl = getContentUrl();
    });    
}
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.