2017-07-25 36 views
0

該所選網頁加載與一個單選按鈕如何隱藏默認一個div是angular組分:
如何設置功能部件通過默認加載​​頁面時隱藏divdiv只有點擊radio-button才能加載。當在角2

`import { Component, OnInit } from '@angular/core'; 

@Component({ 

    templateUrl: './radio-test.component.html', 
    styleUrls: ['./radio-test.component.css'] 
}) 

export class RadioTestComponent { 
    private selectedLink: string="Radio1";   
    setradio(e: string): void 
    { 
    this.selectedLink = e;   
    } 
    isSelected(name: string): boolean 
    { 
     if (!this.selectedLink) { 
     return false; 
    }  return (this.selectedLink === name); 
    } 


} 

` 

Html
如何調用從組件功能,以便在默認情況下沒有div應顯示。

<div class="jumbotron"> 
<div class="radio"> 
      <label> 
       <input type="radio" name="gender" value="Radio" (click)="setradio('Radio1')" > 
       Radio 1 
      </label> 
     </div> 
     <div class="radio"> 
      <label> 
       <input type="radio" name="gender" value="Female" (click)="setradio('Radio2')" ngModel> 
       Radio 2 
      </label> 
     </div> 


<div *ngIf="isSelected('Radio1')" > 
    <div style="height:52px;width:300px;background-color: grey;">Radio button 1 is selected </div> 
    </div> 

    <div *ngIf="isSelected('Radio2')"> 
    <div style="height:52px;width:300px;background-color: lightgrey;">Radio button 2 is selected </div> 
    </div> 

</div> 
+0

爲什麼使用函數u,你可以簡單地做,而無需使用功能 – Ajith

+0

**我怎麼能做到這一點沒有功能。功能是用於顯示Div點擊** –

+0

我的理解是,單擊單選按鈕時顯示div。 – Ajith

回答

0

app.html

<div class="jumbotron"> 
    <div class="radio"> 
    <label> 
     <input type="radio" [(ngModel)]="model.btn" name="btn" value="btn1" 
[checked]="model.btn == btn1" > Radio 1 
    </label> 
    </div> 
    <div class="radio"> 
    <label> 
     <input type="radio" [(ngModel)]="model.btn" name="btn" value="btn2" 
[checked]="model.btn == btn2"> 
      Radio 2 
    </label> 
    </div> 


<div *ngIf="model.btn=='btn1'" > 
    <div style="height:52px;width:300px;background-color: grey;">Radio button 1 
     is selected </div> 
    </div> 
    <div *ngIf="model.btn=='btn2'"> 
    <div style="height:52px;width:300px;background-color: lightgrey;">Radio 
    button 2 is selected </div> 
    </div> 
</div> 

app.ts

import { Component, OnInit } from '@angular/core'; 
    @Component({ 
    templateUrl: './radio-test.component.html', 
    styleUrls: ['./radio-test.component.css'] 
    }) 

export class RadioTestComponent { 
model:any; 
constructor(){ 
     this.model=new Button(); 
     this.model.btn ='btn1'; 
    } 
    } 

class Button { 
    btn: string 
    } 
+0

我可以在我的RadioTestComponent類中使用此代碼 –

+0

@ReetikaSharma U可以在您的組件中使用像這樣更新 – Ajith

+0

非常感謝您的解決方案。 –