0

ng-ifの使い方は?私は以下のコードを持っています。 image_pathがnullでなければtdを表示し、そうでない場合はtdを返します。私のコードは動作しません。値が ""のときにどのように使用するのですか?

NG-IF = "hs.image_path!= """

<td ng-if="hs.image_path != """><img id="customer_photo"src="http://localhost:3000/{{hs.image_path}}" alt="{{hs.image_path}}" width="80" height="50" class="rounded mx-auto d-block"></td> 
以下

あるデータベースあなたが誤って引用符を使用している

{ "_id" : ObjectId("587669fc16d3ef159c4a1463"), 
      "image_path" : "images/job_p.jpg",   
      "details" : "Programmer"   
      "datetime" : ISODate("2017-01-11T17:23:08.870Z") 
     }, 
     { 
      "_id" : ObjectId("58766a4616d3ef159c4a148c"), 
      "image_path" : "",   
      "details" : "Programmer Sleeping",   
      "datetime" : ISODate("2017-01-11T17:24:22.195Z") 
     } 
+3

hs.image_path!= ''(一重引用符)または(hs.image_path.length> 0)を試してください –

+0

一重引用符を使用しようとしましたか? – Mfusiki

+0

答えて

1

。あなたが使用することができます

考えられる解決策:あなたはng-src代わりのsrc

NGと-IFディレクティブの問題のために使用すべきすべての

  1. ng-if="hs.image_path != ''"
  2. ng-if="hs.image_path.trim().length > 0"
+0

@ Nagaveerのコメント –

+0

からコピーします。 –

1

ファースト2つのオプションがあります:

オプション1:

hs.image_pathが ''と異なる場合はtrueです。

<td ng-if="hs.image_path!=''"> 
    <img id="customer_photo" ng-src="http://localhost:3000/{{hs.image_path}}" alt="{{hs.image_path}}" width="80" 
    height="50" class="rounded mx-auto d-block"> 
</td> 

はオプション2:

hs.image_pathの場合はtrueになりますが、その中に値を持っています。

<td ng-if="hs.image_path"> 
    <img id="customer_photo" ng-src="http://localhost:3000/{{hs.image_path}}" alt="{{hs.image_path}}" width="80" 
    height="50" class="rounded mx-auto d-block"> 
</td> 
3

が、これは<td ng-if="hs.image_path != ''">または<td ng-if="hs.image_path.length > 0">

var app = angular.module("app", []); 
 
app.controller("ctrl", function($scope) { 
 
    $scope.var = [{ 
 
    "_id": "587669fc16d3ef159c4a1463", 
 
    "image_path": "AngularJS-large.png", 
 
    "details": "Programmer", 
 
    "datetime": "2017-01-11T17:23:08.870Z" 
 
    }, { 
 
    "_id": "58766a4616d3ef159c4a148c", 
 
    "image_path": "", 
 
    "details": "Programmer Sleeping", 
 
    "datetime": "2017-01-11T17:24:22.195Z" 
 
    }]; 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<div ng-app="app" ng-controller="ctrl"> 
 
    <table> 
 
    <tr ng-repeat="hs in var"> 
 
     <td ng-if="hs.image_path != ''"> 
 
     <img id="customer_photo" ng-src="https://angularjs.org/img/{{hs.image_path}}" alt="{{hs.image_path}}" width="80" height="50" class="rounded mx-auto d-block"> 
 
     </td> 
 
    </tr> 
 
    <table> 
 
</div>

も変更してみsrc=""からng-src=""

<img id="customer_photo" ng-src="http://localhost:3000/{{hs.image_path}}" />

関連する問題