2

I am new to angular 4. I am facing problem in showing JSON in my html view. I have following json.

{"result":[
{"role_id":124,"role_name":"ramesh","roles":"user"},
{"role_id":123,"role_name":"suresh","roles":"admin"}
]
}

component file

export class LoginComponent implements OnInit {
  data: Object;
  loading: boolean;
  credentials: Credentials;
  constructor(private http: Http) { }

  ngOnInit() {
    this.credentials = new Credentials();
  }
  login():void{
    console.log(this.credentials);

    this.loading = true;
    this.data = {};
    this.http.request('http://localhost:8080/api/Sampledata')
      .subscribe((res: Response) => {
        this.data = res.json();
        this.loading = false;
        console.log(this.data)
      });

  }
}

Html File

<h2>Basic Request</h2>
    <button type="button" (click)="login()">Make Request</button>
    <div *ngIf="loading">loading...</div>
    <pre>{{data | json}}</pre>

I am getting the json in front end like this. Front End Image

I want to get role_name separately so i tried changing the html file like this

<h2>Basic Request</h2>
    <button type="button" (click)="login()">Make Request</button>
    <div *ngIf="loading">loading...</div>
    <pre>{{data?.role_name}}</pre>
    </div>

What is the right way to display just the role_name?

5
  • You have multiple role_names, I suppose you'd want to show them all? Commented May 18, 2018 at 8:32
  • What role_name? You have one for each object in your array... Commented May 18, 2018 at 8:32
  • data.result[0]?.role_name Commented May 18, 2018 at 8:35
  • 1
    @Vikas That would have to be data.result[0].role_name Commented May 18, 2018 at 8:36
  • @ Robby Cornelissen Oops My bad I missed it Thanx for pointing it out Commented May 18, 2018 at 8:38

3 Answers 3

1

you can use *ngFor because data is in array of object.

<pre *ngFor="let res of data.result">{{res.role_name}}</pre>
Sign up to request clarification or add additional context in comments.

Comments

0

you can access a particular element using index

<h2>Basic Request</h2>
    <button type="button" (click)="login()">Make Request</button>
    <div *ngIf="loading">loading...</div>
    <pre>{{data.result[0]?.role_name}}</pre>
    </div

and if you want to list out all the names use *ngFor

<h2>Basic Request</h2>
        <button type="button" (click)="login()">Make Request</button>
        <div *ngIf="loading">loading...</div>
         <pre *ngFor="let r of data.result">{{r.role_name}}</pre>
     </div>

DEMO

Comments

0

Do as below to list donw each property of you object

.subscribe((res: Response) => {
        this.data = res.json();
       if(this.data.result && this.data.result.length > 0)
        this.objekeys = Object.keys(this.data.result[0]);
        this.loading = false;
        console.log(this.data)
      });

<table>
   <tr *ngFor="let data of data.result">  
     <td *ngFor="let key of objekeys">
       <span>key </span> <span>data[key]</span>
     </td>
   </tr>
</table>

Comments

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.