Saturday, June 30, 2018

Access custom data attributes value in Angular 4

For accessing custom data attribute, I'll take the example of Dropdown HTML Element:

HTML Code:

<select (change)="onChangeDays($event.target.selectedOptions[0].attributes.data.value)">
      <option value="0" data="0">Select</option>
      <option value="1" data="10">10 Days</option>
      <option value="2" data="20">20 Days</option>
      <option value="3" data="30">30 Days</option>
</select>

Angular Code:

public onChangeDays(dataValue: any) {
         console.log(dataValue);
}

Here whatever option is selected respectively w'll get the data attribute value. Like if we select 10 Days then we get 10.

No comments:

Post a Comment