Lucian
Lucian

Reputation: 354

Angular How to obtain onselect event

I am displaying some data in a smart-table component(using angular). The last column is a select element. What i want to do is plug-in to the "on-select" change event and do something with the data displayed in the row.

Here is my plunker :

What i want to achieve is displayed in the comment

Upvotes: 4

Views: 28685

Answers (2)

Keyur Shah
Keyur Shah

Reputation: 536

If you want to create a Dropdown list its better to use ng-options than ng-repeat.

<select ng-options="x.data for x in myItems"> 
</select>

Because the ngRepeat directive repeats a block of HTML code for each item in an array, it can be used to create options in a dropdown list, but the ngOptions directive was made especially for filling a dropdown list with options, and has at least one important advantage:

Dropdowns made with ng-options allows the selected value to be an object, while dropdowns made from ng-repeat has to be a string.

Add ng-change, as well as the required ng-model, and you pass on the ng-model variable into ng-change, if you need to access its value:

<select ng-change="handleChange(selectedRow)" ng-model="selectedRow" ng-options="x.data for x in myItems"> 
</select>

Then in our controller:

$scope.handleChange = function(row){
    //here take action or do whatever you want to. if you want to access the value, you do it through 'row' here, e.g. console.log(row)


}

Upvotes: 10

Łukasz
Łukasz

Reputation: 2171

If you want trigger function on click on any element use ng-click. It can be used as attribute of element. You can call any function and pass arguments.

<div ng-repeat="myItem in myItems" ng-click="clicked(myItem.data)">{{myItem.data}}</div>

Check plunker to see how it works in your ng-repeat.

It works, but as it stated in another answer:

If you want to create a Dropdown list angular its better to use ng-option then ng-repeat.

Upvotes: 0

Related Questions