jadjoubran / laravel5-angular-material-starter

Get started with Laravel 5.3 and AngularJS (material)
https://laravel-angular.readme.io/
MIT License
1.66k stars 401 forks source link

Directive [$compile] injection, how to? #467

Closed saade closed 7 years ago

saade commented 7 years ago

I need to use $compile inside a directive. But it says: $compile is not defined.

How do i inject $compile?

Code example:

class VppDoPostController {
    constructor($compile) {
        'ngInject';
    }
}

export function VppDoPostDirective() {
    return {
        controller: VppDoPostController,
        scope: {
            model: '='
        },
        replace: true,
        link: function(scope, element, attrs, controllers) {
            var el = angular.element('<medium-editor/>');
            $compile(el)(scope);
            element.append(el);
        }
    }
}
mikemillers commented 7 years ago

I think you need to assign it to a variable on the controller class constructor (eg this.$compile = $compile) then it should be accessible in the link function by doing controllers.$compile

saade commented 7 years ago

@mikemillers you're right! Thanks BRO!!