Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getting the ng-object selected with ng-change

Tags:

angularjs

Given the following select element

<select ng-options="size.code as size.name for size in sizes "          ng-model="item.size.code"          ng-change="update(MAGIC_THING)"> </select> 

Is there a way to get MAGIC_THING to be equal to the currently selected size, so I have access to size.name and size.code in my controller?

size.code affects a lot of the other parts of the app (image urls, etc), but when the ng-model of item.size.code is updated, item.size.name needs to be updated as well for the user facing stuff. I assume that the correct way to do this is capturing the change event and setting the values inside of my controller, but I'm not sure what I can pass into update to get the proper values.

If this is completely the wrong way to go about it, I'd love to know the right way.

like image 876
Patrick Avatar asked Jan 17 '13 19:01

Patrick


People also ask

How do you use NG on change?

The ng-change event is triggered at every change in the value. It will not wait until all changes are made, or when the input field loses focus. The ng-change event is only triggered if there is a actual change in the input value, and not if the change was made from a JavaScript.

How does ng select work?

Definition and Usage. The ng-selected directive sets the selected attribute of an <option> element in a <select> list. The option will be selected if the expression inside the ng-selected attribute returns true. The ng-selected directive is necessary to be able to shift the value between true and false .

Can we use filter in NG-model?

The ng-repeat values can be filtered according to the ng-model in AngularJS by using the value of the input field as an expression in a filter. We can set the ng-model directive on an input field to filter ng-repeat values.

How do I set default Ng-options?

In my opinion the correct way to set a default value is to simply pre-fill your ng-model property with the value selected from your ng-options , angular does the rest. Essentially when you define the $scope property your select will bind to assign it the default value from your data array.


2 Answers

Instead of setting the ng-model to item.size.code, how about setting it to size:

<select ng-options="size as size.name for size in sizes"     ng-model="item" ng-change="update()"></select> 

Then in your update() method, $scope.item will be set to the currently selected item.

And whatever code needed item.size.code, can get that property via $scope.item.code.

Fiddle.

Update based on more info in comments:

Use some other $scope property for your select ng-model then:

<select ng-options="size as size.name for size in sizes"     ng-model="selectedItem" ng-change="update()"></select> 

Controller:

$scope.update = function() {    $scope.item.size.code = $scope.selectedItem.code    // use $scope.selectedItem.code and $scope.selectedItem.name here    // for other stuff ... } 
like image 55
Mark Rajcok Avatar answered Dec 22 '22 04:12

Mark Rajcok


You can also directly get selected value using following code

 <select ng-options='t.name for t in templates'                   ng-change='selectedTemplate(t.url)'></select> 

script.js

 $scope.selectedTemplate = function(pTemplate) {     //Your logic     alert('Template Url is : '+pTemplate); } 
like image 36
Divyesh Rupawala Avatar answered Dec 22 '22 04:12

Divyesh Rupawala