Angular 6 String Interpolation not updating with value inside .subscribe()

六月ゝ 毕业季﹏ 提交于 2019-12-24 07:58:12

问题


I have a component that displays the title and artist of a particular video. When I call playNext() in my service, it's supposed to update the title and artist with a new data. I do a console.log and I could verify that the subscription works. I get an updated value everytime playNext() is called, however the value in the template does not get updated. It only displays correctly the first time. Am I missing something?

Here is my component code

@Component({
  selector: 'ntv-now-playing',
  templateUrl: './now-playing.component.html',
  styleUrls: ['./now-playing.component.css']
})
export class NowPlayingComponent implements OnInit {
  nowPlaying: Observable<Video>;
  video: Video;
  constructor(private videoService: VideoPlayerService) { }

  ngOnInit() {
    this.nowPlaying = this.videoService.getNowPlaying();
    console.log('nowPlayingComponent');
    this.nowPlaying.subscribe(v => {
      this.video = v;
      console.log('nowPlaying:', this.video);
    });
  }
}

In my service, I do this: I removed some of the working code for brevity.

@Injectable()
export class VideoPlayerService {
  ...
  nowPlayingChanged = new Subject<Video>();
  ...
  private nowPlaying: Video;

  constructor(private db: AngularFirestore) {}
  ...

  getNowPlaying(): Observable<Video> {
    return this.nowPlayingChanged;
  }

  playNext(vIdx: number, lastIdx: number) {
    console.log('playNext()');
    this.nowPlaying = this.videos[vIdx];
    console.log(this.nowPlaying);
    this.nowPlayingChanged.next(this.nowPlaying);

  }
}

My template has this:

<div class="video-info">
  <h5>{{video?.title}} - {{video?.artist}}</h5>
  <div>Channel Info&nbsp;&nbsp;<button class="btn btn-secondary">+ Subscribe</button></div>
</div>

Console log inside the component's subscribe function shows this everytime playNext() is called so I know it's changing correctly, it's just the UI does not get updated.

nowPlaying: {artist: "Alan Walker", duration: "3:32", startTime: Timestamp, thumbnailPath: "https://i.ytimg.com/vi/60ItHLz5WEA/hqdefault.jpg", title: "Faded", …}
nowPlaying: {artist: "Deadmau5", duration: "3:37", thumbnailPath: "https://i.ytimg.com/vi/UG3sfZKtCQI/hqdefault.jpg", title: "Monophobia", videoId: "UG3sfZKtCQI"}
nowPlaying: {artist: "Steve Aoki", duration: "59:55", thumbnailPath: "https://i.ytimg.com/vi/sR3w1P2nPH8/hqdefault.jpg", title: "Tomorrowland", videoId: "x9ZkC3OgI78"}
video-player.component.ts:69 

Any advice is appreciated. I've seen some people use ngZone but I heard that might not be the best approach and to be honest it feels kind of hacky.


回答1:


Probably this can help:

@Component({
  selector: 'ntv-now-playing',
  templateUrl: './now-playing.component.html',
  styleUrls: ['./now-playing.component.css']
})
export class NowPlayingComponent implements OnInit {
  nowPlaying: Observable<Video>;
  video: Video;
  constructor(
    private videoService: VideoPlayerService,
    private changeDetector: ChangeDetectorRef
   ) { }

  ngOnInit() {
    this.nowPlaying = this.videoService.getNowPlaying();
    console.log('nowPlayingComponent');
    this.nowPlaying.subscribe(v => {
      this.video = v;
      this.changeDetector.detectChanges();
      console.log('nowPlaying:', this.video);
    });
  }
}


来源:https://stackoverflow.com/questions/52324855/angular-6-string-interpolation-not-updating-with-value-inside-subscribe

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!