2017-06-22 38 views
1

我想在我的應用程序中啓用默認段。我提到了其他線程和問題,解決方案似乎是對組件文件夾中存在的段js進行一些更改。添加離子組件

但是我沒有任何段文件在組件文件夾內。我只是在html頁面中輸入代碼來添加組件。

我一直在做錯了嗎?或者有什麼方法可以添加它?

感謝您的幫助。

編輯1:

<div> 
    <ion-segment [(ngModel)]="testing"> 
    <ion-segment-button value="book">book</ion-segment-button> 
    <ion-segment-button value="table">table</ion-segment-button> 
    </ion-segment> 
</div> 
    <div [ngSwitch]="testing" > 
    <ion-list *ngSwitchWhen="'book'" ngSelected="selected"> 
     <a ion-item> 
     <ion-label>book list</ion-label> 
     </a> 
    </ion-list> 
<ion-list *ngSwitchWhen="'table'"> 
     <a ion-item> 
     <ion-label>table list</ion-label> 
     </a> 
    </ion-list> 
</div> 
+0

請問您可以在代碼中添加最相關的代碼部分嗎? – sebaferreras

+0

你可以添加一些代碼來嘗試幫助你嗎@sebaferreras說? –

+1

添加編輯代碼1. –

回答

0

如果要選擇默認的第一個標籤,你可以在聲明它分配段到testing屬性的值:

@Component({ 
    selector: 'page-home', 
    templateUrl: 'home.html' 
}) 
export class HomePage { 

    public testing = 'book'; 

    // ... 

} 

然後在視圖中:

<div> 
    <ion-segment [(ngModel)]="testing"> 
    <ion-segment-button value="book">book</ion-segment-button> 
    <ion-segment-button value="table">table</ion-segment-button> 
    </ion-segment> 
</div> 
    <div [ngSwitch]="testing" > 
    <ion-list *ngSwitchWhen="'book'"> <!-- I've removed the ngSelected--> 
     <a ion-item> 
     <ion-label>book list</ion-label> 
     </a> 
    </ion-list> 
<ion-list *ngSwitchWhen="'table'"> 
     <a ion-item> 
     <ion-label>table list</ion-label> 
     </a> 
    </ion-list> 
</div> 

解釋很簡單。 <ion-segment [(ngModel)]="testing">結合段組件到testing屬性,<div [ngSwitch]="testing" >將檢查testing屬性的值,看看應該顯示哪些列表:那麼由'book'值從分配給testing財產

<ion-list *ngSwitchWhen="'book'">...</ion-list><ion-list *ngSwitchWhen="'table'">...</ion-list>

你的組件,你會默認選擇第一個段。

如果您想要更改選定的選項卡(以編程方式),您可以執行this.testing = 'table';,並選擇第二個片段。