I'm sorry that I have asked a similar question but I have been struggling with this. As you can see from the getById
is where the array is located on the page.
Here is the typescript file:
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { DashboardService } from '../dashboard.service';
import { UserRole } from 'src/app/models/user';
@Component({
selector: 'app-dashboard',
templateUrl: './dashboard.component.html',
styleUrls: ['./dashboard.component.scss']
})
export class DashboardComponent implements OnInit {
constructor(private activatedRoute: ActivatedRoute, private dashboardService: DashboardService) {}
accessToken = null;
Userid = null;
responseId = null;
UserRoles = null;
ngOnInit() {
this.getById();
this.accessToken = localStorage.getItem('accessToken');
this.Userid = localStorage.getItem('UserId');
}
getById() {
this.dashboardService.getUsersId(1).subscribe((res) => {
console.log(res);
this.responseId = res;
})
}
}
The JSON response I'm trying to receive is like this:
{
"Id": 1,
"Fullname": "test name",
"Username": "test",
"Password": "$2a$10$f.upx2WTxlriul4QK/1GSuCxd6Dmw7.NgVeJ2VfuiC0uIZai5rsD6",
"PasswordSalt": "$2a$10$uCpbT0wAGFx5NxCwlZOkK.",
"Enabled": true,
"Locked": false,
"Deleted": false,
"LogonAttempts": 0,
"LastLogon": null,
"LastModifiedBy": "Tester",
"LastModifiedOn": "2020-09-14T16:08:35.853Z",
"CreatedBy": "Tester",
"CreatedOn": "2020-09-14T16:08:35.853Z",
"UserRoles": [{
"Id": 3,
"Roles": "Admin",
"RolePermissions": [{
"Id": 1,
"Permission": "Administrator"
},
{
"Id": 2,
"Permission": "Currency"
}
]
}]
}
and my HTML page is like this:
<p>Token: {{accessToken}}</p>
<a routerLink="titles" class="btn btn-primary mr-3">Titles</a>
<a routerLink="exercise-questions" class="btn btn-primary">Exercise Questions</a>
<a routerLink="customers" class="btn btn-primary">Customers</a>
<a routerLink="flavours" class="btn btn-primary">Flavours</a>
<p> UserID {{Userid}} </p>
<button class="btn btn-primary mr-3" (click)="getById()">Get By Id</button>
<div class="row">
<div class="col-6">
<h1>Get By Id</h1>
<ul *ngIf="responseId != null">
<li><span>Id: </span>{{responseId.Id}}</li>
<li><span>Fullname </span>{{responseId.Fullname}}</li>
<li><span>Last Logon: </span>{{responseId.LastLogon}}</li>
<li><span>Role Permissions </span>{{responseId.UserRoles}}</li>
</ul>
</div>
</div>
All I wish to do is to save Fullname
, LastLogon
& Role Permisisons
into localStorage and then to be able to display that on the html page.
So far I have been able to display Fullname
, and lastlogon
but haven't been able to store to the localStorage.