2017-04-10 58 views
1

你好,我有一个上传图像的问题。我使用打字稿,所以我试图改编fiddle。我在互联网上发现,但问题是,我没有使用示波器,因此字段'myFile'没有从指令修改。我使用bindToControllerscope:{myFile'='},但它不起作用。角度ControllerAs语法与自定义指令

感谢您的帮助

+1

可以粘贴相关代码到这个问题? jsFiddle可能永远不会存在。 – jHilscher

+0

myApp.directive('fileModel',['$ parse',function($ parse){ return { restrict:'A', link:function(scope,element,attrs){ var model = $ parse(文件模型); var modelSetter = model.assign; element.bind('change',function(){ scope。$ apply(function(){ modelSetter(scope,element [0] .files [0 ]); }); }); } }; }]); – jjijji

回答

1

如果你想使用控制器的语法指令,这样使用

function myExample() { 
    var directive = { 
     restrict: 'EA', 
     templateUrl: '...', 
     scope: { 
      myFile: '=' 
     }, 
     link: linkFunc, 
     controller: ExampleController, 

     controllerAs: 'vm', 
     bindToController: true 
    }; 
return directive 

    function linkFunc(scope, el, attr, ctrl) { 
    scope.vm.myFile = ... 
} 
} 
function ExampleController(){ 
var vm = this 
} 
+0

。$ apply(function(){modelSetter(scope.vm.myFile,element [0] .files [0]);});问题是在这里我改变了,但myFile总是未定义 – jjijji

+0

你在主控制器上尝试console.log'myFile'吗?你在主控制器中使用'$ scope',只需在指令中使用它。在主控制器中尝试更改为'var file;''$ scope.myFile = file' – Akashii

+0

当我制作scope.vm.myFile时,我不能在控制器中使用范围=它可以工作,但是当我尝试\t 。$ apply (function(){modelSetter(scope.vm.myFile,element [0] .files [0]);});这是'undefined' – jjijji