0

I load some json:

[{
        "id": "0a4bf3b5bb5f47ece9284052389ae02f6c9dba989ca34086a30e049ee3d8eb47",
        "name": "Celery",
        "status": "Offline",
        "servicecontrolled": true
},
{
        "id": "ec9471ec001c10b9fa286e1f52e39c5dc9485a7c2cfbf55145c26242bb98ec4d",
         "name": "Nginx",
         "status": "Online",
         "servicecontrolled": false
}]

and show servicecontrolled in html:

 <td>
    <span>{{ service_rec.servicecontrolled }}</span>
 </td>

How to show only "servicecontrolled": true in html code?

1
  • can you please post the complete code Commented Jul 28, 2016 at 14:19

2 Answers 2

2

Edit:

Now that the question is more precise, what you could do is:

<td>
    <span *ngIf="!!service_rec.servicecontrolled">"servicecontrolled": {{ service_rec.servicecontrolled | json }}</span>
</td>

Old answer (thought you wanted to display “true” or “false”): What about:

<td>
    <span>"servicecontrolled": {{ service_rec.servicecontrolled | json }}</span>
</td>

Seems like a trick, but very straightforward…

Or you could write a boolean pipe that would format boolean to string (see https://angular.io/docs/ts/latest/guide/pipes.html).

Something like:

import { Pipe, PipeTransform } from '@angular/core';

@Pipe({name: 'booleantostring'})
export class BooleanToStringPipe implements PipeTransform {
  transform(value: boolean): string {
    return (!!value) ? 'true' : 'false';
  }
}

then, in your html (with the pipe loaded in the pipes key of the @component decorator):

<td>
    <span>"servicecontrolled": {{ service_rec.servicecontrolled | booleantostring }}</span>
</td>
Sign up to request clarification or add additional context in comments.

2 Comments

all also remains, true and false are show, but i would to see only true values
aaaaaaah… ok… I’ve updated my answer (you should be more precise in your question)
1

Your JSON is syntactically incorrect. It should look like:

[
    {
        "id": "0a4bf3b5bb5f47ece9284052389ae02f6c9dba989ca34086a30e049ee3d8eb47",
        "name": "Celery",
        "status": "Offline",
        "servicecontrolled": true
    },
    {
        "id": "ec9471ec001c10b9fa286e1f52e39c5dc9485a7c2cfbf55145c26242bb98ec4d",
         "name": "Nginx",
         "status": "Online",
         "servicecontrolled": false
    }
]

Then you can show it with

<div *ngFor='let value of values' *ngIf='value.servicecontrolled'>
    {{ value.name }}
</div>

3 Comments

yes, i know, because I put only part of my code, but not full
@EduardArevshatyan you might want to fix your question then
I think i know, what you are looking. Take a look to my edit.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.