Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular ng-if change span text

I have this JSON file that I am taking objects as products from. When displaying the sizes of the products I want to change a span from "sizes:" to "kids:" when the json object has "kids": "1".

<div class="sizes-wrap">
        <span class="size-label"><span>sizes:</span>
        <span class="sizes">{{ item.sizes }}</span>
</div>

this code prints Sizes: and the sizes from the json e.g. "128 cm,140 cm,152 cm,164 cm"

I want when in the json object "kids" has a value of 1 to change the word "sizes" to "kids" in the html.

<div class="sizes-wrap">
        <span class="size-label"><span>kids:</span>
        <span class="sizes">{{ item.sizes }}</span>
</div>

Here is an one of the json objects:

  "kids": "0",
  "name": "Product name",
  "sizes": "Small,Medium,Large,X-Large,XX-Large,3XL",
  "kid_adult": "0",
  "free_porto": "0",
  "price": "649,00",
  "package": "0",
  "delivery": "1-2 dage",
  "price_old": "0,00",
  "id": "133714",
  "women": "0"

This is what I wanted to achieve after all:

<div class="sizes-wrap">
        <span ng-if="item.kids == 0 && item.kid_adult == 0 && item.women == 0" class="size-label"><span>sizes:</span></span>
        <span ng-if="item.kids == 1" class="size-label"><span>kids:</span></span>
        <span ng-if="item.kid_adult == 1" class="size-label"><span>adult kids:</span></span>
        <span ng-if="item.kid_adult == 1" class="size-label"><span>women:</span></span>
        <span class="sizes">{{ item.sizes }}</span>
    </div>
like image 713
user1780729 Avatar asked Aug 23 '15 20:08

user1780729


People also ask

How does* ngIf work?

The ngIf directive removes or recreates a portion of the DOM tree based on an {expression}. If the expression assigned to ngIf evaluates to a false value then the element is removed from the DOM, otherwise a clone of the element is reinserted into the DOM.


2 Answers

With a javascript expression, in this case using a ternay operator.

<span>
    {{ user.biography ? user.biography : 'Without information' }}
</span>
like image 64
uruapanmexicansong Avatar answered Oct 11 '22 10:10

uruapanmexicansong


This should work for you:

<div class="sizes-wrap">
    <span class="size-label">
        <span ng-if="item.kids == 0">sizes:</span>
        <span ng-if="item.kids == 1">kids:</span> 
    </span>
    <span class="sizes">{{ item.sizes }}</span>
</div>
like image 44
keithm Avatar answered Oct 11 '22 08:10

keithm