简体   繁体   English

角度订阅更改不会触发视图更新

[英]Angular subscription change doesn't trigger view update

I am having issues with change detection and I cannot figure out whats going wrong, or if I just do not understand Angular properly. 我在变更检测方面遇到问题,我无法弄清楚出了什么问题,或者我只是不正确地了解Angular。 I looked at the stackoverflow answers for similar issues but the methods didn't solve my problem. 我查看了类似问题的stackoverflow答案,但是这些方法无法解决我的问题。

I have a service that is storing a selection of emotions that I have 2 modal components subscribing to. 我有一项服务,该服务存储了我选择的两种情态组件所选择的情绪。 I have one modal in which the user selects emotions, and then the modal uses the service to update the selected emotions. 我有一个模式,用户可以在其中选择情绪,然后该模式使用服务来更新所选的情绪。 The second modal also subscribes to the selected emotions, and updates the list of emotions on its Class when it receives the update. 第二个模态还订阅选定的情感,并在接收到更新时更新其类上的情感列表。 I've tried several methodologies like detectChanges() and tick() to no avail. 我已经尝试了好几种方法,例如detectChanges()和tick()都没有用。 I know that subscription is working and the modal is receiving an updated list of emotions, but somehow change detection isn't working at all and a view update isn't being triggered. 我知道订阅正在工作,并且模态正在接收情绪的更新列表,但是以某种方式根本无法进行更改检测,也不会触发视图更新。 I can't figure out why manually triggering change detection isn't working, which makes me question my understanding. 我不知道为什么手动触发变更检测无法正常工作,这使我质疑我的理解。 I thought if I updated this.emotions in the class, which the view depends on, that would trigger an update.... 我认为如果我在视图所依赖的类中更新this.emotions,则会触发更新。

Service: 服务:

@Injectable()
export class BeforeFormProvider {
  selectedEmotions = new BehaviorSubject({});
  selectedDistractions = new BehaviorSubject({});

  constructor(private databaseProvider: DatabaseProvider) {
  }

  updateEmotions(emotions) {
    this.selectedEmotions.next(emotions);
  }

  updateDistractions(distractions) {
    this.selectedDistractions.next(distractions);
  }

}

Modal where change detection is not working : I'm only going to include the part of the Class where I have the updating logic. 无法检测到更改的模态 :我将仅包含具有更新逻辑的Class部分。 I have emotions as a property on the Class. 作为课堂上的一种属性,我有情感。

ngOnInit() {
    //automatically generate date/time to current for form
    this.date = this.datePipe.transform(new Date(), 'mediumDate');
    this.time = this.datePipe.transform(new Date(), 'shortTime');

    this.emotionsSubscription = this.bfProvider.selectedEmotions.subscribe(emotions => {
      this.emotions = emotions;
      this.ref.tick();
    });
    this.distractionsSubscription = this.bfProvider.selectedDistractions.subscribe(distractions => this.distractions = distractions);
  }

Modal HTML: The p tag is not updating, it never recognizes any change! 模态HTML: p标签不会更新,它永远不会识别任何变化! I can't figure out what i'm doing wrong. 我不知道我在做什么错。

<ion-item detail-push (click)="openEmotionsList()">
   <h2>Emotions</h2>
   <p>{{emotions | getValues}}</p>
</ion-item>

I figured this out. 我想通了。 I needed to make a new copy of the list before sending via .next() in the service. 在通过服务中的.next()发送之前,我需要制作列表的新副本。 I was saving a reference to the object returned via subscribe in the Modals, and then sending a mutated version of the same object back with every update from the service, and therefore it didn't trigger a view update. 我在模态中保存了对通过subscribe返回的对象的引用,然后随着服务的每次更新将相同对象的变异版本发送回去,因此它不会触发视图更新。

updateEmotions(emotions) {
  this.selectedEmotions.next({...emotions});
}

Instead of making a copy, look at your getValues pipe, make sure it is not set to pure: 要查看副本的getValues管道,请确保未将其设置为pure:

@Pipe({
  name: 'getValues',
  pure: false
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM