45

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.

3
  • can you provide a fiddle?
    – Ronnie
    Commented Oct 16, 2013 at 16:37
  • Seems to work just fine if it's as simple as this demo. I'd say there is something else interfering with your code.
    – Yoshi
    Commented Oct 16, 2013 at 16:47
  • @Yoshi I figured as much. I'm not seeing any errors thrown in the console. I will do some deeper debugging now that I can see I am using ng-options/ng-model correctly. Commented Oct 16, 2013 at 16:59

2 Answers 2

125

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.

10
  • 16
    I was using an ng-if in a higher block, which was creating a new scope. Thank you for the help. Commented Oct 16, 2013 at 18:15
  • I was having my select inside ng-include which created a new scope, your solution worked Commented Jan 28, 2014 at 19:52
  • ng-switch creates a new scope also, as does ng-repeat. Commented Jan 28, 2014 at 20:55
  • 5
    Wow, this insight was incredibly helpful. This is hard to diagnose when Angular implicitly creates a child scope. Thank you. Commented Mar 17, 2014 at 8:03
  • 2
    If it helps anyone, I found my scope was nested two levels deep. Try ng-model="$parent.$parent.myVariable" to see. What is the advantage of creating a child scope?
    – hipnosis
    Commented Oct 17, 2015 at 13:24
1

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>
2
  • This didnt really help. Commented Nov 28, 2017 at 3:36
  • Do you realize that this answer is 3 years old? xD
    – Chris
    Commented Nov 29, 2017 at 8:45

Not the answer you're looking for? Browse other questions tagged or ask your own question.