2017-09-15 52 views
0

我是新來的角。我在Angular 4中(點擊)遇到問題。(點擊)不會觸發。試圖尋找解決方案。什麼都沒有解決。以下是我在HTMLAngular 4(點擊)內部不會觸發* ngIf

<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; 
    } 
    } 

回答

3

格式化代碼,你將看到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方法內

+0

嗨拉胡爾,感謝您的回覆。對於那個很抱歉。我在類中添加了Add()方法。還是一樣。它沒有解僱 – user2083386

+0

@ user2083386當你把它放回類中時,如果有什麼錯誤,你也可以在'Add()'方法中添加一個控制檯日誌 –

+0

即使在瀏覽器控制檯中,我也沒有收到任何錯誤。 Add()按鈕點擊不會觸發。我的要求是,當我點擊添加按鈕,添加方法需要被解僱,這不是正在發生的 – user2083386