mkmnstr mkmnstr - 1 year ago 94
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

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>

Any ideas?

Thanks a lot everyone

Answer Source

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> 

See ng-value.

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download