我有一個服務,通過組件訂閱的observable。這似乎在訂戶顯示初始值時起作用。我有另一個組件,然後更新observable但是新值沒有顯示。Angular 4訂閱observable不會更新後更新
服務:
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/of';
@Injectable()
export class BannerService {
banners$: Observable<any[]> = Observable.of([]);
getBanners(): Observable<any[]> {
return this.banners$;
}
setBanners(banners: any[]): void {
this.banners$ = Observable.of(banners);
}
}
組件與用戶:
import { Component, ViewEncapsulation, OnInit } from '@angular/core';
import { BannerService } from './../banner/banner.service';
@Component({
selector: '.banner',
templateUrl: './banner.component.html',
styleUrls: ['./banner.component.sass'],
encapsulation: ViewEncapsulation.None
})
export class BannerComponent implements OnInit {
constructor(private bannerService: BannerService){}
ngOnInit() {
this.bannerService.banners$.subscribe(banners => {
console.log(banners);
});
}
}
組件與二傳:
import { Component, ViewEncapsulation, OnInit } from '@angular/core';
import { BannerService } from './../banner/banner.service';
@Component({
selector: '.home-component',
templateUrl: './home.component.html',
styleUrls: ['./home.component.sass'],
encapsulation: ViewEncapsulation.None
})
export class HomeComponent implements OnInit {
data = {
banners: [
{
title: 'Title 1',
image: '../images/image1.jpg'
},
{
title: 'Title 2',
image: '../images/image2.jpg'
},
{
title: 'Title 3',
image: '../images/image3.jpg'
}
]
}
constructor(private bannerService: BannerService){}
ngOnInit(): void {
this.bannerService.setBanners(this.data.banners);
}
}
任何幫助將非常感激,我已經嘗試了一堆不同事情,並不能得到它的工作。
發行不與此修復程序解決,誰能幫助https://stackoverflow.com/questions/48973734/model-updates-but-angular-ui-does -not-rerender – kasimkha