Rory
Rory

Reputation: 4320

Call function before calling template in Angular directive?

I'd like to call a function passing it one of the scope variables below, but I'm getting the error:

ReferenceError: activityLog is not defined

Just wondering if what I'm trying to do here is possible? My function is getting called OK, but I want to be able to pass it the scope variable as well.

function (angular, jQuery, oModule) {
    'use strict';

    oModule.directive('myDirective', function () {
        return {
            restrict: 'E',
            scope: {
                activityLog: '@',
            },
            // Why can't I pass activityLog here?
            // ReferenceError: activityLog is not defined
            template: generateDetails(activityLog)
        };
    });

    function generateDetails(oActivityLog) {

    }

Upvotes: 0

Views: 199

Answers (1)

whyyie
whyyie

Reputation: 792

You should get the activityLog scope in link function. Template is for you to have html template if there is.

function (angular, jQuery, oModule) {
    'use strict';

    oModule.directive('myDirective', function () {
        return {
            restrict: 'E',
            scope: {
                activityLog: '@',
            },
            link:function(scope, element, attr) {
                generateDetails(scope.activityLog);
            },
            template: '<div>123</div>' //html template here if needed
        };
    });

    function generateDetails(oActivityLog) {

    }
}

Upvotes: 1

Related Questions