question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Don't call ngf-change with an empty files parameter

See original GitHub issue

@danialfarid What I’m seeing is that ngf-change is being called TWICE. Once when you click the button and the file chooser dialog opens and once when the dialog closes after selecting a file.

I added this to your fiddle…

    $scope.upload = function (files) {
        alert('called from upload. files is: ' + files);

Whatever is calling it shouldn’t call it the first time. There is no reason to call $scope.upload if files is null. This would allow me to remove the null check and simplify things greatly.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:20 (17 by maintainers)

github_iconTop GitHub Comments

1reaction
Harlantrcommented, May 26, 2017

2017 update:

I have a form that is set up to validate on whether or not a file was uploaded. When a user would cancel out of the dialog box, the upload field would be invalid because the ng-model had changed to null.

Since the API has since changed from the suggestions in this issue but this page is still the top result when searching “ng-file-upload change empty,” I figured I’d note my solution:

I simply hooked into the ngf-before-model-change function, set a temporary controller variable, and reset my “real” value when ngf-change was called. It looks a little like this:

form.html

<a id="fileUpload"
     href=""
     name="fileUpload"
     class="fa fa-2x fa-upload"
     ng-model="ctrl.file"
     ngf-select
     ngf-min-size="1"
     ngf-change="ctrl.uploadFile($file)"
     ngf-before-model-change="ctrl.setTmpFile($file)"
     required>
</a>

controller.js

setTmpFile (newFile) {
    // If new file exists, set the temporary variable
    // newFile will be null if the user cancels out of the dialog box
    if(newFile){
        this.tmpFile = newFile;
    };
};

uploadFile (newFile, index) {
    // Set the "real" model to the temporary file
    this.file = this.tmpFile;

    ...
    Do other stuff
};

Now, the ng-model variable ctrl.file is either a real file (the user selected one in the dialog) or the file it was previously (the user cancels out of the box).

0reactions
v29neilcommented, Jun 12, 2015

Had the same issue- Fixed it by using. Not sure if this is the correct way <button ngf-select="" ng-model="files" ngf-multiple="false" >Upload </button>

In controller- $scope.$watch('files', function (files) { console.log(files); if( (files != null) ){ console.log('file uploading'); } });

Read more comments on GitHub >

github_iconTop Results From Across the Web

AngularJS: ng-change called before an select element is ...
try ng-change="ngChange" instead of ng-change="ngChange()"? I think the ngChange() is making a call. – NPToita. Dec 2, 2015 at 7:20.
Read more >
Top 18 Most Common AngularJS Developer Mistakes - Toptal
Sheer size of the AngularJS can easily lead to many mistakes. In this article you will learn about most common AngularJS developer mistakes...
Read more >
deploy — AWS CLI 1.27.37 Command Reference
Deploys the specified AWS CloudFormation template by creating and then executing a change set. The command terminates after AWS CloudFormation executes the ...
Read more >
Makefile Tutorial By Example
The goal of Makefiles is to compile whatever files need to be compiled, based on what files have changed. But when files in...
Read more >
about Functions Advanced Parameters - PowerShell
Get-Date_Func: Cannot process argument transformation on parameter ... and Set-Content cmdlets only when it's used in a file system drive.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found