Access fileitem from child controller on parent controller in AngularJS

Issue

I am using angular file uploader in child component and need to access the fileitem when onAfterAddingFile is fired. I have implemented binding in component. So far, I have tried this-

Childcontroller:

    $onInit() {

    this.feedFileInfo = 'abc';//here it is updated
    this.uploader.filters.push({
        name: 'customFilter',
        fn: function(item /*{File|FileLikeObject}*/, options) {
            return this.queue.length < 10;
        }
    });
this.uploader.onAfterAddingFile = function(fileItem) {
        console.log('fileItem');
        this.feedFileInfo = 'xyz';//this value is not being updated to feedFileInfo variable and hence cannot be obtained in parent controller
        console.info('onAfterAddingFile', fileItem);
    };

I need the updated value ie. fileitem in this variable.
Any guidance would be appreciated.

Solution

You use the this from the declared function, not the outer one.
One classical workaround is to use:

var that = this;
this.uploader.onAfterAddingFile = function (fileItem) {
  that.feedFileInfo = 'xyz';
};

Answered By – PhiLho

Answer Checked By – Cary Denson (AngularFixing Admin)

Leave a Reply

Your email address will not be published.