简体   繁体   English

Angular - *ng如果不评估

[英]Angular - *ngIf not evaluating

I'm attempting to troubleshoot an issue and have written the following code to see what is going on, but now I am even further confused.我正在尝试解决问题并编写以下代码以查看发生了什么,但现在我更加困惑。 Ideally if entry.customer has a value it should be printed and then Hello should also be displayed, otherwise NOPE and Goodbye should be displayed, but the actual result is that only the value of entry.customer is displayed.理想情况下,如果entry.customer有值应该打印,然后也应该显示Hello ,否则应该显示NOPEGoodbye ,但实际结果是只显示entry.customer的值。

<h3>{{entry.customer}}</h3>

<span *ngIf="entry.customer else no">
  <h1>Hello</h1>
</span>

<ng-template #no>
  <h1> NOPE</h1>
</ng-template>

<span *ngIf="!entry.customer">
  <h1>Goodbye</h1>
</span>

<h3> {{entry.customer}} </h3>

<span *ngIf="entry.customer else no">
  <h1>Hello</h1>
</span>

<ng-template #no>
  <h1> NOPE</h1>
</ng-template>

<span *ngIf="!entry.customer">
  <h1>Goodbye</h1>
</span>

When I started tracking down this issue I thought that maybe there was an issue with change detection due to the value of entry coming from a BehaviorSubject, so I attempted getting the values in a zone, but that didn't change the result.当我开始追踪这个问题时,我认为由于条目的值来自 BehaviorSubject,可能存在更改检测问题,因此我尝试获取区域中的值,但这并没有改变结果。


ngOnInit(): void {
   this.ngZone.run(() => {
     debugger;
     this._entryService.entry.subscribe(entry => {
       debugger;
       this.entry = entry;
     });
   })
 }

Can anyone explain a possible reason why *ngIf won't work, yet using the curly braces does display the value?谁能解释 *ngIf 不起作用的可能原因,但使用花括号确实显示了该值? If it helps any the component that I am using is extended from a base component that gets the value of entry as there are many components of the same type in this application.如果它有帮助,我正在使用的任何组件是从获取 entry 值的基本组件扩展而来的,因为此应用程序中有许多相同类型的组件。

You may have used changeDetection: ChangeDetectionStrategy.OnPush in your component in that case try to trigger changeDetection manually ChangeDetectorRef您可能在组件中使用了changeDetection: ChangeDetectionStrategy.OnPush在这种情况下尝试手动触发 changeDetection ChangeDetectorRef

constructor(
    private _entryService: EntryService,
    private changeDetectionRef: ChangeDetectorRef
  ) {}

  ngOnInit(): void {
    this._entryService.entry.subscribe(entry => {
      this.entry = entry;
      this.changeDetectionRef.detectChanges();
    });
  }

This is the issue with syntax.这是语法的问题。 Semicolon is missed in between.中间漏掉了分号。 You should write你应该写

<span *ngIf="entry.customer; else no">
  <h1>Hello</h1>
</span>

You can also use "then" or "then else"您也可以使用“then”或“then else”

<span *ngIf="entry.customer; then content">
  <h1>Hello</h1>
</span>

The issue turned out to be that I had added the component to the module, but hadn't added the module to the app.module.问题原来是我已将组件添加到模块中,但没有将模块添加到 app.module 中。 Thanks to @raj m for this answer that led me to the solution.感谢@raj m 的回答让我找到了解决方案。 https://stackoverflow.com/a/42066452/2793683 https://stackoverflow.com/a/42066452/2793683


<span *ngIf="entry.customer; else no">
  <h1>Hello</h1>
</span>
<ng-template #no>
  <h1> NOPE</h1>
</ng-template>

; ; [ semicolon] is missed in your code in <span *ngIf="entry.customer; else no"> <span *ngIf="entry.customer; else no"> 您的代码中缺少 [分号]

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

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