Я изучаю Angular 8 служб и наблюдаемых с этим API (https://cat-fact.herokuapp.com/facts). Проблема в том, что я не могу получить доступ к данным в «Все». Я использую интерфейс с ответом http.
Если я попытался вернуть data.all, визуальный код выдаст мне предупреждение " Свойство 'all' не существует для типа 'IFact []'.".
В представлении, если я попытался получить доступ к data.all у меня есть эта ошибка:
ОШИБКА TypeError:" _co.fact is undefined "
Интерфейс:
export interface IFact {
_id: string,
text: string,
type: string,
user: {
_id: string,
name: {
first: string,
last: string,
}
},
upvotes: number,
userUpVoted: ''
};
Сервис:
@Injectable({
providedIn: 'root'
})
export class FactService {
private _url = 'http://localhost:4200/facts';
constructor(private http: HttpClient) { }
getFacts(): Observable<IFact[]>{
return this.http.get<IFact[]>(this._url)
}
}
Компонент:
export class FactComponent implements OnInit {
public facts = [];
constructor(private _factService: FactService) { }
ngOnInit() {
this._factService.getFacts()
.subscribe(
data => {
this.facts = data;
console.log(this.facts);
},
err => console.log(err)
)
}
}
HTML
<table>
<td ng-repeat="fact of facts">
<tr>{{fact._id}}</tr>
</td>
</table>