angularjs - 在自定义编译的html上不调用$ destroy

时间:2014-09-11 13:10:40

标签: javascript angularjs

我做了一个指令,编译了一个' div'使用ng-include和ng-controller,并将其附加到元素。它工作正常,但由于某种原因," $ destroy"当控制器被销毁时,不会调用该事件。

我做的正确吗?

这是html:

<my-Directive tr-model="someBindedObject"></my-Directive>

这是JS:

angular.module('myModule').
directive('myDirective', function ($compile, controllerMapper) {

    function compileTemplate(scope, element, attrs) {

        var controller = controllerMapper.getController(scope.model);

        innerHtml = '<div ng-include src="' + "'" + controller.templateUrl + "'" + '"  ng-controller="' + controller.controllerName + '" ></div>';

        var innerElement = $compile(innerHtml)(scope);
        element.empty();
        element.append(innerElement);

    };

    return {
        scope: {
            model: "=trModel"
        },
        link: compileTemplate
    }
}).
controller('myController', function($scope){
    $scope.$on('$destroy', function () {
        //this is never called
    });
});

0 个答案:

没有答案