0
我與角2的實驗,我試着用內置的HTTP服務的HTTP請求:角2查看HTTP調用後
@Injectable()
export class MyService {
constructor(private http: Http) {
}
public get(): Observable<string[]> {
return this.http.get('http://localhost:8080/endpoint')
.map((data) => <string[]>data.json()['content']);
}
}
用法如下:
export class MyComponent implements OnInit {
public content: string[];
constructor(private myService: MyService) {
}
public ngOnInit(): void {
this.myService.get().subscribe((data) => {
this.content = data;
});
}
}
如果我使用zone
來運行數據分配,那麼它的工作方式與預期的一樣,但是我覺得這有點不方便,我不明白爲什麼它不起作用。
我讀過代碼可能在角度區域之外運行,但我不明白爲什麼,因爲我只使用內置功能。
查看綁定:
<div *ngFor="let str of content">
{{ str }}
</div>
能否請你幫忙嗎?
謝謝!
你的視圖綁定是什麼樣的。在這個例子中,我也沒有看到你需要'zone.run(...)'的原因。 –
我也沒有,這就是爲什麼我不明白爲什麼它不工作。我已經添加了視圖綁定。 – galovics
如果您添加'