You should declare a public property in the class to be able to use it in the template
articles
refers to a service injected privately, so it cannot be accessed from the component template, and it is a service instance and you cannot use it to store articles in. you should use a different variable for that purpose.
you can declare a public property allArticles
in your ArticlesComponent
component, assign to it the result from your service, and this way it can be accessed from the template.
you can do this way:
import { Component, OnInit } from '@angular/core';
import {ArticleService} from '../../services/article.service';
@Component({
selector: 'app-articles',
templateUrl: './articles.component.html',
styleUrls: ['./articles.component.css']
})
export class ArticlesComponent implements OnInit {
public allArticles;
constructor(private articles : ArticleService) { }
ngOnInit(){
this.articles.getAll().subscribe( results => {
this.allArticles = results;
});
}
}
in the template:
<h3>Repositorios : {{allArticles.articlesCount}}</h3>
In your service getAll()
should return an Observable
import { Observable } from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class ArticleService {
public articlesCount : number = 30;
constructor(private http: HttpClient) { }
public getAll(): Observable<any> {
return this.http.get('https://api.github.com/users/codigofacilito/repos');
}
}
in your ArticleService
, you have to remove the subscribe in getAll()
as well
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…