【问题标题】:how to use ng-if condition with strings?如何对字符串使用 ng-if 条件?
【发布时间】:2016-03-08 07:14:47
【问题描述】:

我收到来自 web 服务的响应为“未找到附件”和“找到附件”。我想在一个页面上显示 2 个不同的图像,一个在没有找到附件的情况下,另一个在发现附件的情况下。

这是我的html:

  <div  style="float:left">
        <img src="img/icon_doc_active.png " style="width:20px" ng-if="hasdocument(response == "attachment found")" ng-click="findDoc() "></img>

       <img src="img/icon_doc_inactive.png " style="width:20px" ng-if="!hasdocument(response=="no attachment found")"></img>
   </div>

这是我的控制器:

  $scope.responseofdoc = [];

    $scope.findDoc = function() {
                    $scope.urlObj = [];
                    $scope.docCheck = false;
                    $scope.docListCheck = false;
                    $scope.cLoader = true;
                    $scope.docMsg = "No Attachment found";
                    $scope.docModalOpen = true;
                    attachmentService.fetchAttachmentId(temp).then(function(response) {
                            console.log(response);
                            if (response == "No Attachments found") {
                                console.log("my response");
                                $scope.responseofdoc.push(response == "No Attachments found");
                                console.log( $scope.responseofdoc);
                                $scope.docMsg = response;
                                $scope.docCheck = true;
                                $scope.cLoader = false;
                                // $scope.docListCheck = false;
                            } else {
                                console.log("found attachment");
                                console.log("my response");
                                $scope.docListCheck = true;
                                var data = JSON.parse(response);
                                //  $scope.urlObj = [];
                                getDocBase64(data);
                            }
                        },
                        function(error) {
                            console.log(error);
                            $scope.docMsg = "Error while fetching attachment";
                            $scope.docCheck = true;
                            $scope.cLoader = false;
                        });

                }

我将如何检查 $scope.responseofdoc = []; 中的字符串响应在我的 html 中带有 ng-if 条件的数组并显示所需的文档图像?

【问题讨论】:

  • 试试这个 -> ng-if="hasdocument(response == 'attachment found')"
  • 我是否应该创建另一个函数来检查响应的值 bcz 我使用它到 findDoc 函数中。并将他的值推入 $scope.responseofdoc 数组?

标签: javascript angularjs ionic-framework factory


【解决方案1】:

在你的 html 中尝试:

<div  style="float:left">
  <img src="img/icon_doc_active.png " style="width:20px" ng-if="hasdocument(response == 'attachment found')" ng-click="findDoc() "></img>

  <img src="img/icon_doc_inactive.png " style="width:20px" ng-if="!hasdocument(response == 'no attachment found')"></img>
</div>

当您使用双引号时,它与您用于属性的双引号冲突。因此,当您使用双引号时,请与单引号进行比较,反之亦然。

【讨论】:

    【解决方案2】:

    您关闭 HTML 中元素的 ng-if 属性。您应该使用单引号:

    ng-if="hasdocument(response == 'attachment found')"
    

    更改这两个元素,它应该工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-20
      相关资源
      最近更新 更多