我有一个检查可观察值的函数,并基于该值执行一些逻辑来更改我在服务中定义的变量的值。一切工作正常,除了更改后的值未通过字符串插值在Web组件中呈现(更新)。它已在服务中正确更改(当我console.log正确返回时),但由于某种原因而无法获取它来更新组件。我已经阅读了很多有关ngZone,ChangeDetectorRef等的信息,并在过去曾遇到过更新问题的其他领域实施了这些策略,但是由于某些原因,它们在这里不起作用。在下面的代码中,如果我将头顶撞了一段时间,任何指导将不胜感激。
//From the component where I'm performing the checks on the observable. The component where I'm doing the string interpolation on the data service value is a different component
ngOnInit(){
this.myObservable$ = this.scanitservice.decodedString$.subscribe(data => {
if (
data ==
this.adventuredata.exhibit[this.adventuredata.adventure.currentAmount]
.target
) {
this.adventuredata.sharedVariables.guideText =
'You found the right clue! Great job! Answer the question correctly to claim your prize.';
console.log(this.adventuredata.sharedVariables.guideText);
this.showQuiz = true;
this.changeDetector.detectChanges();
console.log('Evaluated as matched');
}
});
}
//From the data service
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class AdventuredataService {
constructor() {}
sharedVariables = {
guideText: '',
quizText: ''
};
}
<div class="card-body float-right m-0 p-0">
<img
src="{{ this.adventuredata.adventure.guideImage }}"
alt=""
class="card-img-top w-25 float-left"
/>
<span class="card-text p-0">
{{ this.adventuredata.sharedVariables.guideText }}
</span>
</div>