Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS: Select not 2-way binding to model

I am using a select to show Client names. A user should be able to select an existing client, which will then update the scope property:

Controller

Initializing the "first pick".

if($scope.clients.length > 0) $scope.existingClient = $scope.clients[0]; 

View

<select     id='nm-existing-client-name'     class='form-control  input-lg'     ng-model='existingClient'     ng-options="client.name for client in clients"> </select> 

The scope property existingClient does not change when the select menu changes. If no value is initialized (controller line above is removed), the value of existingClient will stay undefined.

Attaching an ng-change will fire when a value changes, but the model itself will not update to the new value.

I am using AngularJS v1.2.0-rc.3.

like image 992
Tyler Shaddix Avatar asked Oct 16 '13 16:10

Tyler Shaddix


People also ask

Does AngularJS support two way binding?

AngularJS creates a two way data-binding between the select element and the $ctrl.

How do I bind a model in AngularJS?

Two-way Binding Data binding in AngularJS is the synchronization between the model and the view. When data in the model changes, the view reflects the change, and when data in the view changes, the model is updated as well.

Does AngularJS use one-way data binding?

One-way data binding in AngularJS means binding data from Model to View (Data flows from the scope/controller to the view). 'ng-bind' is an angular directive used for achieving one-way data binding.

Which object is used for two way data binding in AngularJS?

Without $scope object performs two-way binding in AngularJS. Advantages of AngularJS. Difference between One-way and Two-way data binding.


2 Answers

I think you are probably using a child scope and don't know it. ng-if, ng-repeat, ng-switch and ng-include all create child scopes. Values on your scope are inherited, but if you change the value in a child scope it sets a value on the child scope and leaves the inherited value unchanged on the parent. Try using an object instead to hold your values and see if that fixes it. Since you are only setting a property on an object and not a value directly on the scope it will use the parent scope's inherited object and update the value.

$scope.data = {     existingClient: $scope.clients.length > 0 ? $scope.clients[0] : undefined }; 

View:

<select ng-model="data.existingClient"          ng-options="client.name for client in clients"> </select> 

You can use the AngularJS Batarang extension in chrome to help debug your scopes.

like image 62
Jason Goemaat Avatar answered Oct 11 '22 18:10

Jason Goemaat


This is also a solution to keep parameters into your $scope object:

controller:

$scope.scope = $scope; $scope.clients = []; $scope.existingClient = $scope.clients.length > 0 ? $scope.clients[0] : undefined; 

view:

<select ng-model="scope.existingClient" ng-options="client.name for client in clients"></select> 
like image 43
Chris Avatar answered Oct 11 '22 18:10

Chris