Angular调用API后不重新渲染HTML组件
创始人
2024-10-24 12:01:24
0

使用Observable来订阅API返回结果,并在返回结果后更新组件视图

示例代码如下:

组件:

import { Component, OnInit } from '@angular/core'; import { ApiService } from './api.service';

@Component({ selector: 'app-my-component', template:

  • {{ item }}
}) export class MyComponent implements OnInit { items: string[];

constructor(private apiService: ApiService) { }

ngOnInit() { this.apiService.getItems().subscribe(items => { this.items = items; }); } }

API服务:

import { Injectable } from '@angular/core'; import { HttpClient } from '@angular/common/http'; import { Observable } from 'rxjs/Observable';

@Injectable() export class ApiService {

constructor(private http: HttpClient) { }

getItems(): Observable { return this.http.get('https://my-api.com/items'); } }

相关内容

热门资讯

透视中牌率!hhpoker透视... 相信很多朋友都在电脑上玩过hhpoker透视方法吧,但是很多朋友都在抱怨用电脑玩起来不方便。为此小编...
透视存在!德普之星辅助正版,a... 透视存在!德普之星辅助正版,aapoker透视脚本,重大发现(有挂介绍),亲,有的,ai轻松简单,又...
透视规律!hhpoker万能辅... hhpoker万能辅助器赢率提升策略‌;透视规律!hhpoker万能辅助器,fishpoker透视,...
透视神器!红龙poker透视,... 透视神器!红龙poker透视,aapoker辅助是真的吗,重大通报(有挂教学);是一款可以让一直输的...
透视私人局!hhpoker辅助... 透视私人局!hhpoker辅助软件下载,we-poker有人玩吗,高科技教程(一直是真的有挂);亲真...
透视玄学!wpk脚本是什么,a... 1、透视玄学!wpk脚本是什么,aapoker免费透视脚本,热点讨论(有挂规律);详细教程。2、aa...
透视了解!aapoker辅助插... aapoker辅助插件工具赢率提升策略‌;透视了解!aapoker辅助插件工具,wpk辅助哪里买,系...
透视安卓版!aapoker透视... 透视安卓版!aapoker透视怎么用,aapoker辅助工具免费下载,推荐几款新版(有挂规律)1、构...
透视软件!aapoker辅助插... 《透视软件!aapoker辅助插件工具,约局吧德州真的存在透视吗,规律教程(都是存在有挂)》 aap...
透视游戏!wepoker透视脚... 透视游戏!wepoker透视脚本安卓,wpk辅助器安装,分享开挂内幕(有挂攻略)1、打开德州poke...