3

In Angular2 is there a way for developer to check if the corresponding element of one component is tagged with some attribute/class or not?

Say we have below html:

<outer-controller>
    <label label-directive class='dynamically-set-class' dynamically-set-attribute>just some label</label>
</outer-controller>

The label-directive marked in the label element is a Angular2 component, the two dynamically-set-class and dynamically-set-attribute are dynamically controlled by the outer-controller basing on user input.

Every time the outer-controller reset the class of the label or the attribute, it fire a event that caught by label-directive and so the label-directive should do something dynamically base on the run-time attribute/class setting.

However, I cannot find any example in existing official Angular2 doc

4

1 回答 1

4

@Input() should do what you want:

@Directive({
  selector: '[label-directive]'
})
export class LabelDirective {
  @Input('class.dynamically-set-class') _dynamicallySetClass: boolean;
  @Input('dynamically-set-attribute') _dynamicallySetAttribute: any;

  ngOnInit() {
    console.log('class ' + _dynamicallySetClass);
    console.log('attribute ' + _dynamicallySetAttribute);
  }
  ngOnChanges(changes) {
    ..
  }
}
于 2016-01-22T07:52:27.420 回答