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?
role_names, I suppose you'd want to show them all?role_name? You have one for each object in your array...data.result[0]?.role_namedata.result[0].role_name