【问题标题】:ng-show / ng-hide removes ng-model from $scopeng-show / ng-hide 从 $scope 中删除 ng-model
【发布时间】:2016-11-11 22:35:38
【问题描述】:
  <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>

我正在使用 ng-croppie 指令。当我隐藏包含 ng-croppie 的 div 时,我裁剪的 url 在 $scope 中消失了。

它来自:

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

然后,一旦调用了 ng-show,就:

pictureUrl:"data:,"

如果我取消隐藏 div,pictureUrl 会按预期返回。

如何使这个 pictureUrl 属性即使在隐藏时也能保持不变?

我知道 ng-if 会从 dom 中删除元素,但这对于 ng-show 来说似乎是意料之外的......

【问题讨论】:

    标签: javascript angularjs ng-show angular-ng-if ng-hide


    【解决方案1】:

    您的 ng-show 指令位于您的 ng-croppie 控件的父 DIV 上。 ng-show 切换 HTML 控件的显示,这意味着当 ng-show 条件表达式不满足时,您的整个 div(及其内容)将被隐藏,包括 DIV 子项。您需要将您的 ng-croppie 移出此 DIV 或将您的 ng-show 指令放置在不包含 ng-croppie 控件的其他位置。

    【讨论】:

    • 最好的方法是什么?我需要隐藏cropper / croppie,除非用户决定他们需要在提交之前再次编辑照片。我想我可以放置条件来检查$watch中的pictureUrl并将它存储在一个变量中,但似乎有点hacky ..
    • 我猜ng-croppie 控件可以采用ng-change 指令,默认情况下ng-change 指令在给定模型的输入值更改提交给模型时触发,在您的'post.pictureUrl' 的案例。您可以只提供一个 onChange 事件处理程序方法来将模型的分离副本存储到 $scope 中的局部变量中。我猜ng-change 指令也可能在ng-show 修改您的模型时被调用,因此您需要使用一些逻辑来验证您的模型字符串的内容到您的事件处理程序方法中。
    • 这可能是更角度的尝试方式。我最终将裁剪照片数据作为副本存储在一个变量中,而不仅仅是一个参考——使用 JSON.parse(JSON.stringify(pictureUrl));
    猜你喜欢
    • 2016-09-11
    • 2014-12-08
    • 2014-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-30
    • 2015-02-12
    • 1970-01-01
    相关资源
    最近更新 更多