2017-04-10 104 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