1

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.

3
  • 1
    ng-if creates a new scope for the dom elements div. Use ng-show instead Commented Aug 23, 2017 at 5:02
  • Try changing ng-if to ng-show Commented Aug 23, 2017 at 5:02
  • check this stackoverflow.com/a/29669339/1608841; may be possible duplicate Commented Aug 23, 2017 at 5:03

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you. Which is the better solution?
Controller as since it implicitly takes care of the dot rule.But you might have to change the code at many places. The easiest one will be to use ng-show or bind your file-model to an object like file-model="file1.attachmentDL" and in your controller use $scope.file1={};
Is it working now? You can read the reference given in the answer to understand about JavaScript Prototypal Inheritance.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.