简体   繁体   中英

Access fileitem from child controller on parent controller in AngularJS

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.

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';
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM