2

I have a directive that loads a template and compiles it:

function question($templateRequest, $compile) {
        return {
            restrict: 'E',
            scope: {
                apply: '&apply',
                item: '=item'
            },
            bindToController: true,
            link: function (scope, element, attrs) {
                //debugger;
                var templateUrl = 'someRandomUrl'
                $templateRequest(templateUrl).then(function (template) {
                    $compile(element.html(template).contents())(scope.$parent);
                }, function () {
                    // An error has occurred
                    $compile(element.html("error").contents())(scope);
                });
            }
        }
}   

The template is correctly generated, I can also call functions from the template html to myController like:

ng-click='vm.apply()'

On myController I am using controllerAs vm syntax and when calling normal functions from the controller view I can access the vm variable inside the scope. Functions called from the template don't have access to the vm variable inside the body.

Template is generated under myController view.

I also have tried using different options for directive configuration, as in removing isolated scope, removing bindToController, etc.

Is it possible? How?

(I know I can add it as an extra argument but wanted to avoid that)

Thanks

  • You've told it to bindToController but haven't provided a controller. You could just add `controller: function(){ this.apply = function(){alert('doing apply');}}` and it would probably work as expected. – toxaq Apr 22 '17 at 22:51

0 Answers0