2016-07-14 6 views
0

selectフォームコントロールをレンダリングするAngular 2コンポーネントを作成しようとしていますが、これをサポートするためにいくつか問題がありますoptgroup角度2のoptgroupをサポートするドロップダウンコンポーネントを選択

レンダリング時に、アイテムに子があるかどうかによって、optgroupoption要素の間で切り替えることができる必要があります。これらの項目を繰り返し処理するためにtemplateタグを使用しようとしていますが、selectの内部ではサポートされていないようです。また、同じ要素に*ngIf*ngForを追加しようとしましたが、どちらもサポートされていません。

selectoptgroupで作成するにはどうすればよいですか?


Plunkr demo

フォームselect.component.js

import { Component, Input, Output, EventEmitter, ElementRef } from '@angular/core'; 
import { CORE_DIRECTIVES, FORM_DIRECTIVES, NgClass, NgStyle } from '@angular/common'; 

export class FormSelectOption { 
    id: string; 
    text: string; 
    children: FormSelectOption[]; 

    constructor(id: string, text: string, children: FormSelectOption[]) { 
     this.id = id; 
     this.text = text; 
     this.children = children; 
    } 
} 

@Component({ 
    selector: 'form-select', 
    template: ` 
    <select> 
     <!-- WORKING --> 
     <option *ngFor="let item of items" (click)="select(item.id)">{{item.text}}</option> 

     <!-- NOT WORKING --> 
     <!--<template *ngFor="let item of items"> 
      <optgroup *ngIf="item.children" label="{{item.text}}"> 
       <option *ngFor="let child of item.children" (click)="select(child.id)">{{child.text}}</option> 
      </optgroup> 
      <option *ngIf="!item.children" (click)="select(item.id)">{{item.text}}</option> 
     </template>--> 
    </select> 
    ` 
}) 
export class FormSelectComponent { 
    @Input() 
    items: FormSelectOption[]; 

    @Input() 
    selectedValue: string[]; 

    @Output() 
    valueChange: EventEmitter<any>; 

    constructor(private elementRef: ElementRef) { 
     this.valueChange = new EventEmitter<any>(); 
    } 

    select(id) { 
     this.valueChange.emit(id); 
    } 
} 

app.ts

//our root app component 
import {Component} from '@angular/core' 
import {FormSelectComponent, FormSelectOption} from './form-select.component'; 

@Component({ 
    selector: 'my-app', 
    template: ` 
     <form-select [items]="things" [selectedValue]="selectedThing"></form-select> 
    `, 
    directives: [FormSelectComponent], 
}) 

export class App { 
    selectedThing = '1'; 
    things = [ 
     new FormSelectOption('1', 'Fruit', [ 
      new FormSelectOption('2', 'Bananas'), 
      new FormSelectOption('3', 'Apples'), 
      new FormSelectOption('4', 'Oranges') 
     ]), 
     new FormSelectOption('5', 'Countries', [ 
      new FormSelectOption('6', 'England'), 
      new FormSelectOption('7', 'New Zealand') 
     ]), 
     new FormSelectOption('8', 'Shapes', [ 
      new FormSelectOption('9', 'Square') 
     ]), 
    ]; 
} 

答えて

3

次のことを試してみてください。

<ng-template ngFor let-item [ngForOf]="items"> 
    <optgroup *ngIf="item.children" label="{{item.text}}"> 
    <option *ngFor="let child of item.children" (click)="select(child.id)"> 
     {{child.text}} 
    </option> 
    </optgroup> 
    <option *ngIf="!item.children" (click)="select(item.id)">{{item.text}}</option> 
</ng-template> 

Plunker

構文ngFor:

<li *ngFor="let item of items; let i = index">...</li> 
<li template="ngFor let item of items; let i = index">...</li> 
<ng-template ngFor let-item [ngForOf]="items" let-i="index"><li>...</li></ng-template> 

https://angular.io/docs/ts/latest/api/common/index/NgFor-directive.html#!#syntax

+0

urghh ...よくそれは迷惑なんです。ありがとう! – ajbeaven

+0

@yurzui私は同じシナリオで打たれてしまった.....ウルのプランカーは働いていない。あなたはそれを更新することができます –

+0

@ PsycheGenie更新https://plnkr.co/edit/xYvGPAhFFpTCPcotqm9F?p=preview – yurzui

関連する問題