Angular 4(单击)不会在* ngIf内部触发

angular

384 观看

2回复

25 作者的声誉

我是新手。我在Angular 4中遇到(单击)问题。(单击)不会触发。试图寻找解决方案。什么都没解决。以下是我的HTML代码

<div class="col col-12 col-spacing">
  <div>
  <md-select [placeholder]="result" [(ngModel)]="selectedItemType">
    <md-option *ngFor='let attr of result' [value]="attr.fieldType" ng-selected="attr.fieldType"> {{attr.attribute}}
    </md-option>
  </md-select>
  </div>
  <div *ngIf="selectedItemType =='string' || selectedItemType =='decimal' || selectedItemType == 'text' || selectedItemType == 'integer'">
    <input placeholder="Enter Text" type="text" class="input" [(ngModel)]="txtEntered">
  </div>
  <div>
    <div *ngIf="selectedItemType == 'date'" class="col col-2 col-spacing">
      <md-input-container class="datepicker-align">
        <input mdInput [mdDatepicker]="startDatepicker" placeholder="Select Date" name="StartDate" id="txtStartDate" [(ngModel)]="date"
          #startDate>
        <button id="btnOpnStartDate" mdSuffix [mdDatepickerToggle]="startDatepicker"></button>
      </md-input-container>
      <md-datepicker #startDatepicker></md-datepicker>
    </div>
  </div>
  <div *ngIf="selectedItemType == 'boolean'">
      <input type="checkbox" />
  </div>
  <button *ngIf="selectedItemType" md-raised-button (click)= "Add()" color="accent" >Add</button>
</div>

这是我的后端/打字稿代码

class TestClass implements OnInit {
    itemSelected: AttrSelectedList;
    chkBox: boolean = false;
    selectedItemType: string = null;
    date: any = null;
    test: any;
    txtEntered: any;
    result: Array<FormatHeader> = [];

    constructor() { }

    handleOnClose() {
      this.dialogRef.close();
    }
    ngOnInit(): void { here we have logic}

    Add() {
      this.test = this.txtEntered;
    }
  }
作者: user2083386 的来源 发布者: 2017 年 9 月 15 日

回应 2


3

11601 作者的声誉

格式化代码后,您将看到该Add方法在类之外。请提供适当的缩进

export class TestClass implements OnInit {
  itemSelected: AttrSelectedList;
  chkBox: boolean = false;
  selectedItemType: string = null;
  date: any = null;
  test: any;
  txtEntered: any;
  result: Array<FormatHeader> = [];

  constructor() { }
  handleOnClose() {
    this.dialogRef.close();
  }
  ngOnInit(): void { here we have logic}
}
Add() { // outside class
  this.test = this.txtEntered;
}
} // unnecessary

add在类中添加此方法

作者: Rahul Singh 发布者: 2017 年 9 月 15 日

1

3674 作者的声誉

创建了punker。使用ngIf在同一时间点击作品。@Rahul Singh是对的。

作者: alexKhymenko 发布者: 2017 年 9 月 15 日
32x32