AngularJS指令加载控制器

时间:2018-10-23 15:34:24

标签: angularjs controller directive

我正在尝试使用指令,ng-include和model参数动态加载控制器,但是它不起作用:

Hmtl:

<ng-include src="obj_data.modulo_appendice"
                dyn-controller="obj_data.modulo_appendice_controller"
                dyn-controller-url="obj_data.modulo_appendice_controller_url"></ng-include>

指令:

WebCauApp.directive('dynControllerUrl', ['$compile', '$parse', function ($compile, $parse)
{
    return {
        restrict: 'A',
        terminal: true,
        priority: 200000,
        link: function (scope, elem)
        {
            // Parse the scope variable
            var controller = $parse(elem.attr('dyn-controller'))(scope);
            var controller_url = $parse(elem.attr('dyn-controller-url'))(scope);
            elem.removeAttr('dyn-controller');
            elem.removeAttr('dyn-controller-url');

            var script = document.createElement('script');
            script.type = "text/javascript";
            script.setAttribute('src', controller_url);

            //var head = angular.element(document.querySelector('head'));
            //head.append(script);
            elem[0].parentNode.before(script);
            elem.attr('ng-controller', controller);
            $compile(elem)(scope);

        }
    }
}]);

错误是参数'mycontrollername'不是函数,未定义

0 个答案:

没有答案