Niranjan Godbole
Niranjan Godbole

Reputation: 2175

Angularjs file upload not working inside ng-if

Hi I am developing web application in angularjs. I have file upload module. I have below html code.

<div ng-if="FilepathDL == ''">
  <input type="file" file-model="attachmentDL" name="attachmentDL" class="form-control" />
  </div>
  <div ng-if="FilepathDL != ''">                       
  <a ng-href="" ng-click="openfile(FilepathDL)">{{ 'View File' | translate }}</a>
  </div>

On page load i am not assigning any values to FilepathDL so first ng-if statement should render. This is happening. After uploading file when submitting the form $scope.attachmentID is "" If i remove ng-if and directly put then i am getting file. May i know what is happening if i put my file control inside ng-if? Any help would be appreciated. Thank you.

Upvotes: 1

Views: 731

Answers (1)

Vivz
Vivz

Reputation: 6620

ng-if creates a child scope. So the file-model is not directly attached to the controllers scope.

You can avoid the problem by any one of the following methods

  1. You can use controller as syntax
  2. You can avoid this by following the dot rule, that is define your file-model on an object.
  3. Use ng-show instead of ng-if

For more reference : https://github.com/angular/angular.js/wiki/Understanding-Scopes

Upvotes: 1

Related Questions