1

Hello I have a problem in uploading an image .I use typescript so I tried to adapt a fiddle. I found on the internet but the problem is that I don't use the scope so the field 'myFile' is not being modified from the directive.I used the bindToController and scope:{myFile'='} but it doesn't work.

Thanks for your help

xkeshav
  • 53,360
  • 44
  • 177
  • 245
jjijji
  • 89
  • 1
  • 9
  • 1
    Can you paste the relevant code into the question? the jsFiddle might not exist forever. – jHilscher Apr 10 '17 at 10:23
  • myApp.directive('fileModel', ['$parse', function ($parse) { return { restrict: 'A', link: function(scope, element, attrs) { var model = $parse(attrs.fileModel); var modelSetter = model.assign; element.bind('change', function(){ scope.$apply(function(){ modelSetter(scope, element[0].files[0]); }); }); } }; }]); – jjijji Apr 10 '17 at 10:39

1 Answers1

1

If you want use controller syntax for directive , use like this

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
}
Akashii
  • 2,251
  • 3
  • 17
  • 29
  • .$apply(function(){ modelSetter(scope.vm.myFile, element[0].files[0]); }); the problem is here i changed but myFile is always undefined – jjijji Apr 10 '17 at 10:56
  • do you try console.log `myFile` in main controller ? And you use `$scope` in main controller , just use it in directive . In main controller try change to `var file;` `$scope.myFile = file` – Akashii Apr 10 '17 at 11:28
  • I don't work with scope in controller when i make scope.vm.myFile=something it works but when i try .$apply(function(){ modelSetter(scope.vm.myFile, element[0].files[0]); }); it is 'undefined' – jjijji Apr 10 '17 at 12:12
  • Can you create a fidde or plnkr ? – Akashii Apr 10 '17 at 12:32
  • I am working with typescript i think that types of 'element' in link function is not correct i put element:any i wrote link(scope:IScope,element:any,attr;IAttributes) – jjijji Apr 10 '17 at 13:09