小编Sag*_*ina的帖子

使用"controller as ..."语法时,在另一个指令中的第三方指令

这是重新创建我的问题的代码片段:

HTML:

...
<div ng-controller="worker as workerCtrl">
    <my-directive label="label" controllerAs="workerCtrl" function="fileUpload(e, this.options)"></my-directive> 
</div>
Run Code Online (Sandbox Code Playgroud)

控制器:

...
function fileUpload(file, options){...}
...
Run Code Online (Sandbox Code Playgroud)

指示:

function myDirective($compile) {

    var directive = {
        restrict: 'E',
        link: link
    };
    return directive;

    function link(scope, element, attrs) {
        var htmlText = '<lx-file-input ' +
            'change="'+attrs.controllerAs+'.'+attrs.uploadFunction+'" ' +
            'label="'+attrs.label+'"></lx-file-input>';
        element.replaceWith($compile(htmlText)(scope));
    }
}
Run Code Online (Sandbox Code Playgroud)

应该是:('lx-file-input'是第三方指令......)

<lx-file-input change="workerCtrl.fileUpload(e, this.options)" label="someLabel"</lx-file-input>
Run Code Online (Sandbox Code Playgroud)

问题是Angular编译和链接的时间关闭,模板留下HTML字符串而不是编译的函数.我知道如果我将控制器设置在指令中但它希望它在HTML中的相同"workerCtrl"范围内使用相同的函数,它将起作用.

javascript angularjs angularjs-directive lumx

5
推荐指数
1
解决办法
189
查看次数

标签 统计

angularjs ×1

angularjs-directive ×1

javascript ×1

lumx ×1