0

I have the following angular directive.

// The component panel
app.directive('component', function () {       
return {
    restrict: 'AE',

    scope: {

        heading: '@heading',
        componentType: '@componentType'


        getComponentUrl: function() {

        var componentPath = "./shell/partials/components/" + componentType + ".html";
        return componentPath;

    }
},


    template: '\
    <div id="panel" class="panel panel-primary panel-component fill scrollable">\
        <div class="panel-heading">\
            <div class="row">\
                <div class="col-sm-6">\
                    <h2 class="panel-title">{{heading}}</h2>\
                </div>\
                <div class="col-sm-6">\
                    <button type="button" class="btn btn-default pull-right btn-expand" data-toggle="tooltip" data-placement="left" title="Go fullscreen" ng-click="">\
                    <i class="fa fa-expand"></i></button>\
                </div>\
            </div>\
        </div>\

        <div class="panel-body fill">\

            <!-- include is used here as different component types may have different attributes\
            so includes are placed here and the directives used within the includes.\
            -->\

            <div ng-include="getComponentUrl()"> </div>\

        </div>\
    </div>\
    ',

    link: function(scope, elm, attrs) {

     }
}
});

As you can see I use the following in the directive's scope:

getComponentUrl: function() { ... }

This doesn't seem to work.

I am trying to supply my ng-include with a predefined string. I appreciate that there are other ways to do this but I wondered i Angular have a means of putting methods into the scope of a directive?

Many Thanks, Kiran

1
  • 1
    use the link function, where you receive that scope and then add the method there Commented Apr 1, 2015 at 13:27

2 Answers 2

1

try

app.directive('component', function () {       
return {
    restrict: 'AE',
    link : function(scope){
       scope.getComponentUrl = function() {

        var componentPath = "./shell/partials/components/" + componentType + ".html";
        return componentPath;

    }
    },
    scope: {

        heading: '@heading',
        componentType: '@componentType'



},
Sign up to request clarification or add additional context in comments.

2 Comments

Ah that seems obvious now but I couldn't see it at the time. Thank you!
sure... sorry forgot!
0

You have to move getComponentUrl: function() { ... } to the link directive method. Add this method to a $scope attributes. And make reference to this function within the template .

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.