mkmnstr mkmnstr - 13 days ago 6
AngularJS Question

AngularJS - select tag - getting attribute from option when selected

So I have a select tag that shows some options and I wish that when an option is selected it takes the

data-something
attribute and pastes its value (of the attribute) in the input element I have. It is only demonstrative, value attribute should be send in the form.

<input ng-model="refreshedByExample" type="text">
<select ng-model="example">
<option value="1" data-something="valueForRefreshedByExample1">1</option>
<option value="2" data-something="valueForRefreshedByExample2">2</option>
<option value="3" data-something="valueForRefreshedByExample3">3</option>
</select>


Any ideas?

Thanks a lot everyone

Answer

Updating the answer with correct code

<input ng-model="refreshedByExample" type="text">
<select ng-model="example" ng-change="refreshByExample=example"> 
    <option value="1" ng-value="valueForRefreshedByExample1">1</option> 
    <option value="2" ng-value="valueForRefreshedByExample2">2</option> 
    <option value="3" ng-value="valueForRefreshedByExample3">3</option> 
</select>

See ng-value.

Also consider using ng-options if your values are contained in a array.

Comments