Search code examples
javascriptangularjsng-showangular-ng-ifng-hide

ng-show / ng-hide removes ng-model from $scope


  <div ng-show="!showTagging" class="form-group">
              <div class="form-group">
                <div ng-show="!showPreview"> Add a photo of you enjoying a specific whiskey or an image inspired by one!</div>
                <hr></hr>

  //when file is uploaded below, it sets $scope.post.picturePreCrop

                <label id="upload" required ng-model="post.picturePreCrop" ngf-pattern="image/*" ngf-select="droppedFile()" accept="image/*" ngf-accept="'image/*'" ngf-pattern="'.jpeg,.jpg,.png,.bmp'" ngf-max-size="8MB" ngf-max-files="1" ngf-fix-orientation="true">
                      <div class="add-link"><i class="fa fa-camera"></i><a class="underlined"> Add or Take a Photo:</a></label></div>
              <div></div>
              <ng-croppie ng-if="post.picturePreCrop" src="post.picturePreCrop | ngfDataUrl" ng-model='post.pictureUrl' update='onUpdate' boundry="{w: 200, h: 200}" viewport="{w: 200, h: 200}" mousezoom="true" zoom="true" type="square">
              </ng-croppie>
              <img ng-if="!post.picturePreCrop" class="prof-photo" ng-src="{{post.picture || 'https://placehold.it/200x200'}}">
            </div>

//once picture is cropped, it sets $scope.post.pictureUrl

            <div class="btn btn-lg btn-block btn-success" ng-disabled="post.picturePreCrop === undefined" ng-click="showTagging = true"> Next </div>

`

//when the next button above is hit, $scope.post.pictureUrl is removed, but post.picturePreCrop still persist.
</div>

i'm using the ng-croppie directive. When I hide the div containing ng-croppie my cropped url disappears in $scope.

it goes from:

pictureUrl:"data:image/png;base64,iVBORw0KGgoAAAAN...

Then, once ng-show is invoked, to:

pictureUrl:"data:,"

If I un-hide the div, pictureUrl returns to as it is expected.

How do I make this pictureUrl property persist even when it's hidden?

I know ng-if removes elements from the dom, but this seems unexpected with an ng-show..


Solution

  • Your ng-show directive is placed on the parent DIV of your ng-croppie control. ng-show toggle the display of an HTML control, meaning that your entire div (and it's content) will be hidden when the ng-show condition expression is not satisfied, including the DIV children. You need to move your ng-croppie out of this DIV or place you're ng-show directive somewhere else that doesn't include the ng-croppie control.