簡體   English   中英

如何在字符串中使用ng-if條件?

[英]how to use ng-if condition with strings?

我收到來自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條件的數組,並顯示所需的文檔圖像?

在您的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>

當您使用雙引號時,它與您用於屬性的雙引號沖突。 因此,當您使用雙引號與單引號進行比較時,反之亦然。

關閉HTML元素中的ng-if屬性。 您應該使用單引號:

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

更改這兩個元素,它應該起作用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM