I am out of practice and trying to brush up on Angular. I want to return an object and render it in the HTML file.
service:
@Injectable()
export class NameService {
url:string = "";
constructor(private http: HttpClient) { }
public getActivities(): Observable<UserModel> {
return this.http.get(this.url);
}
}
component:
export class NameComponent implements OnInit {
users:UserModel;
constructor(private nameService: NameService) { }
public getUsers(){
this.users = this.nameService.getActivities()
.subscribe(res => {this.users = res});
}
ngOnInit() {
this.getUsers();
}
error Type 'Subscription' is not assignable to type 'UserModel'.
and
Type 'Observable<Object>' is not assignable to type 'Observable<UserModel>'.
HTML:
<ul>
<li *ngFor="let user of users; let i = index">
{{i + 1}} - {{user.id}}
{{i + 1}} - {{user.first_name}}
{{i + 1}} - {{user.last_name}}
{{i + 1}} - {{user.email}}
{{i + 1}} - {{user.ip}}
</li>
</ul>
and the JSON:
{
"id": 11,
"first_name": "Blakelee",
"last_name": "Gillingham",
"email": "bgillinghama@engadget.com",
"gender": "Female",
"ip_address": "234.175.225.8"
}, {
"id": 12,
"first_name": "Tally",
"last_name": "Loraine",
"email": "tloraineb@marriott.com",
"gender": "Female",
"ip_address": "50.20.33.244"
},
I am using Angular 5 but using angular 4 practices because it will break my other application in do so.
-----------------Update 1-------------------
I added users:UserModel[]=[];
Then I added
public getActivities(): Observable<UserModel[]> {
return this.http.get<UserModel[]>(this.url);
}
The ide stopped complaining but now I am back the the error:
No provider for HttpHandler!
------------------Edit 2------------------------