【问题标题】:show blank page when use Directive ngIf or Directive ngFor in angular在角度中使用指令 ngIf 或指令 ngFor 时显示空白页
【发布时间】:2019-11-21 09:50:40
【问题描述】:

我刚开始使用 Angular 和初学者。

当我在我的组件中使用 *ngIf 或 *ngFor 时,程序输出是一个空白页面但没有 *ngIf 和 *ngFor 页面可以正常工作。

Product.Component.html的代码

<p>Q: what is your Name?<input [disabled]="isButtonDisable" type="text" #txtAnswer>
    <button [disabled]="product.price>1000" (click)="onclickHandler(txtAnswer.value)">Answer</button>
    <h3 *ngIf="isButtonDisable">{{deadline}}</h3>
</p>

product.component.ts的代码

import { Component } from '@angular/core';

@Component({
  selector: 'app-product',
  templateUrl: './product.component.html',
  styleUrls: ['./product.component.css']
})
export class ProductComponent {
  public isButtonDisable = false;
  public product = {
    name: 'car',
    price: 1000
  };
  public deadline = 20;
  constructor() {
    setInterval(() => this.deadline--, 1000);
    setInterval(() => this.isButtonDisable = true, 20000);
  }
  onclickHandler(Value: number) {
    this.deadline = 20;
  }
}

我的 IDE 是 vscode 和

Angular CLI:8.1.1

节点:10.15.2

操作系统:win32 x64

角度:8.1.0

请指导我哪里错了:(

【问题讨论】:

标签: angular visual-studio-code angular-directive ngfor angular-ng-if


【解决方案1】:

问题是,将&lt;h3&gt;&lt;input&gt; 标签放在&lt;p&gt; 中时会出现验证错误,至少控制台是这么说的……

试试:

Q: what is your Name?
<input [disabled]="isButtonDisable" type="text" #txtAnswer />
<button [disabled]="product.price>1000" (click)="onclickHandler(txtAnswer.value)">
  Answer
</button>
<h3 *ngIf="isButtonDisable">{{deadline}}</h3>

应该渲染得很好。无论如何,您都不需要&lt;p&gt; 标签。你可以通过:host.css文件中设置你的主机组件样式。

【讨论】:

    猜你喜欢
    • 2018-04-25
    • 1970-01-01
    • 2016-08-04
    • 2018-06-01
    • 1970-01-01
    • 2019-02-13
    • 1970-01-01
    • 2020-11-15
    • 2018-12-30
    相关资源
    最近更新 更多