I have what I think should be a pretty simply problem to solve in Angular 2, but I just can't find the right syntax.
My goal is a directive that does the following:
[my-data]="[1,2,3,4,5]"onChange() { return this.data.length = 55; }.I'm fine on 1), but a little lost on 2) and 3). So far I have something like:
@Directive({
selector: ['ap-data'],
host: {
'(change)': 'onChange()'
}
})
export class DataDirective {
@Input('ap-data') data: any;
@HostBinding('attr.ap-data') get dataSet() {
return processData(this.data);
}
...
}
@Component({
selector: 'myComponent',
directive: [DataDirective],
template: `
<div [ap-data]="[1,2,3,4,5]"></div>
`
})
export class MyComponent {
public data: any[];
public dataSet: ProcessedDataType;
...
}
For directive to host binding use an EventEmitter:
@Output('ap-data-change') apDataChange: EventEmitter = new EventEmitter()
onChange() {
...
this.apDataChange.next(someValue);
}
<div [ap-data]="[1,2,3,4,5]" (ap-data-change)=hostField=event$></div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With