1

So, I have the following working code:

Angular:

$scope.audit = {user: ''};

HTML:

<md-select ng-model="audit.user"">
    <md-option ng-repeat="u in users" value="{{u.username}}">
        {{u.username}}
    </md-option>
</md-select>

But, previously I was trying to bind as follows.

Angular:

$scope.auditUser = '';

HTML:

<md-select ng-model="auditUser">
    ..
</md-select>

I read a bit online about not binding to primitives within ng-repeat but nothing conclusive. So I was wondering why the latter solution does not work and why do we have to bind our model to objects when using ng-repeat, is it something to do with the $scope and $digest cycles (of which I don't know in much detail)?

Thanks

2
  • You could try ng-options. Commented Sep 13, 2016 at 12:53
  • Just wondering - ng-value="u.username" might work better. Commented Sep 13, 2016 at 14:02

1 Answer 1

1

Angular creates a new scope for each object in the ng-repeat directive. If that object is actually a primitive, it is passed by value rather than by reference. Therefore, any changes made in that child scope won't be propagated back to the original object in the parent scope.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.