Full working example. I did not find a suitable way to apply ControlValueAccessor to <paper-dropdown-menu> , but instead added it to the hugged <paper-listbox> . The only drawback is that you may need another ControlValueAccessor if you use different content for <paper-dropdown-menu> , the advantage is that you can use ControlValueAccessor with <paper-listbox> , even if it is not wrapped in <paper-dropdown-menu>
import { Component, Directive, Renderer, forwardRef, Provider, ElementRef, ViewEncapsulation, } from '@angular/core'; import {NG_VALUE_ACCESSOR, ControlValueAccessor} from '@angular/common';
const PAPER_MENU_VALUE_ACCESSOR = new Provider( NG_VALUE_ACCESSOR, {useExisting: forwardRef(() => PaperMenuControlValueAccessor), multi: true}); @Directive({ selector: 'paper-listbox', host: {'(iron-activate)': 'onChange($event.detail.selected)'}, providers: [PAPER_MENU_VALUE_ACCESSOR] }) export class PaperMenuControlValueAccessor implements ControlValueAccessor { onChange = (_:any) => { }; onTouched = () => { }; constructor(private _renderer:Renderer, private _elementRef:ElementRef) { console.log('PaperMenuControlValueAccessor'); } writeValue(value:any):void {
@Component({ selector: 'my-app', directives: [PaperMenuControlValueAccessor], encapsulation: ViewEncapsulation.None, template: ` <h2>Hello {{name}}</h2> <paper-menu> <paper-item>Item 1</paper-item> <paper-item>Item 2</paper-item> </paper-menu> <paper-dropdown-menu label="Dinosaurs" > <paper-listbox class="dropdown-content" [(ngModel)]="selected"> <paper-item *ngFor="let item of items">{{item}}</paper-item> </paper-listbox> </paper-dropdown-menu> <div>selected: {{items[selected]}}</div> `, }) export class AppComponent { items = ['allosaurus', 'brontosaurus', 'carcharodontosaurus', 'diplodocus']; selected = 3; name:string; constructor() { this.name = 'Angular2 (Release Candidate!)' } ngAfterViewInit() { //this.selected = this.diplodocus; } }
Plunger example
Update
I found a similar answer for PaperDropdownMenu instead of PaperListbox Bind angular 2 model for a dropdown
Günter zöchbauer
source share