Angular 5 'inputClick' 已声明,但其值从未被读取

Angular 5 'inputClick' is declared but its value is never read

我是 angular 和前端编程的新手,如果这太简单了,我很抱歉。

我有一个 HTML 文件,看起来像这样

<input class="born-date-input" formControlName="dateOfBorn" minlength="1" maxlength="10" (click)="inputClick=true"/>
<div class="alert alert-warning" *ngIf="inputClick==true">
      <small>show warning</small>
</div>

在我的组件 .ts 文件中有

export class MainInputFormComponent implements OnInit, DoCheck {
  @Output('changeData') sendTheNewDataJSON = new EventEmitter<any>();

  private inputClick: boolean = false;
}

在 HTML 文件中没有看到 inputClick 变量

如果将其声明为私有,则不会发生数据绑定。您需要将其声明为 public 才能使其可用。按照下面的代码:

   export class MainInputFormComponent implements OnInit, DoCheck {
      @Output('changeData') sendTheNewDataJSON = new EventEmitter<any>();

      public inputClick: boolean = false;
    }

然后你可以像这样在你的 HTML 中使用它。

<input class="born-date-input" formControlName="dateOfBorn" minlength="1" maxlength="10" (click)="inputClick=true"/>
<div class="alert alert-warning" *ngIf="inputClick">
      <small>show warning</small>
</div>

将 'private' 更改为 'public' 或删除访问修饰符(默认为 public)

export class MainInputFormComponent implements OnInit, DoCheck {
 @Output('changeData') sendTheNewDataJSON = new EventEmitter<any>();

 inputClick: boolean = false;
}

您可以执行其中任一操作,因为当 inputClick 声明为私有时无法访问它

 export class MainInputFormComponent implements OnInit, DoCheck {
 @Output('changeData') sendTheNewDataJSON = new EventEmitter<any>();

 inputClick: boolean = false;
}

export class MainInputFormComponent implements OnInit, DoCheck {
 @Output('changeData') sendTheNewDataJSON = new EventEmitter<any>();

 public inputClick: boolean = false;
}