angularjstypescriptbindingcontrolleras

Angular ControllerAs syntax with custom directive


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


Solution

  • 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
    }