【问题标题】:NgIf not displaying even though expression is true即使表达式为真,NgIf 也不显示
【发布时间】:2020-08-05 00:01:12
【问题描述】:

我想根据 'item' 值显示特定的 html 代码。为此,我使用 *ngIf 指令复制了 10 个 ng 容器。前 9 个工作正确,但最后一个不想显示,即使表达式为真。

<ng-container *ngIf="item == '1'">
    *works*
</ng-container>
//...2,3,4,5,6,7,8,
<ng-container *ngIf="item == '9'">
    *works*
</ng-container>
<ng-container *ngIf="item == '10'">
   //Do not want to display even if item has '10' value
</ng-container>

最有趣的是,即使我在最后一个 ng-container 中输入*ngIf="true" 它仍然不想显示数据

component 

ngOnInit() {
    this.context = this.router.url.replace('/faq/sections/', '').slice(0, -2);
    this.item = this.route.snapshot.params['id'];
  }
<ng-container *ngIf="context == 'instructions'">
    <ng-container *ngIf="item == '1'"></ng-container>
    //...2,3,4,5,6,7,8,9
   <ng-container *ngIf="item == '10'">
    <h2>text</h2>
    <div class="row">
      <div class="col-md-4 p-3 text-center">
        <img src="assets/money-outside.svg" alt="qiwi">
        <p class="mt-3">text</p>
      </div>
      <div class="col-md-4 p-3 text-center">
        <img src="assets/phonenmoney.svg" alt="qiwi">
        <p class="mt-3">text</p>
      </div>
      <div class="col-md-4 p-3 text-center">
        <img src="assets/inst-q-3.svg" alt="qiwi">
        <p class="mt-3">text</p>
      </div>
      <div class="col-md-4 p-3 text-center">
        <img src="assets/book-in-6.svg" alt="qiwi">
        <p class="mt-3">text</p>
      </div>
    </div>
   </ng-container>
</ng-container>

【问题讨论】:

  • 以上代码中没有任何内容可以表明问题出在哪里。您需要为我们提供更多代码以帮助您解决问题。能否提供完整的视图代码?
  • 这是一个工作版本来证明上面的 HTML 不是问题:stackblitz.com/edit/angular-s7edcc
  • @Muirik 添加了更多详细信息
  • @Muirik Guys,对不起,我很愚蠢。 “上下文”变量中的问题。我正在使用 .slice(0, -2) 从 '../instructions/9' 单个数字中删除,只留下字符串 'instructions',现在 10 有 2 个数字,它变成了 'instructions/' 所以我没有' t 传递第一个带有上下文的 ngIf
  • @AlvinSaldanha 发现错误

标签: javascript angular


【解决方案1】:

添加另一个“=” 改成这样: *ngIf="context === '说明'"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-14
    • 1970-01-01
    相关资源
    最近更新 更多