1

I would like to use the AngularJS interpolation binding with dot notation.

<div ng-repeat="item in items">
  <input type="checkbox" ng-model="form.city.{{item.region}}.enabled">
</div>

The ng-model gives me errors. What am I doing wrong?

5
  • 5
    ng-model="form.city[item.region].enabled" should work Commented Oct 6, 2017 at 19:05
  • That worked, thanks! Commented Oct 6, 2017 at 19:07
  • @emed you should add that as an answer Commented Oct 6, 2017 at 19:15
  • @MarcusH I though this question would be closed as a typo, thats why I put in a comment Commented Oct 6, 2017 at 20:52
  • make it so with your votes Commented Oct 6, 2017 at 20:52

2 Answers 2

3

The ng-model value is considered an angular expression, so there is no need to use braces.

ng-model="form.city[item.region].enabled" should work fine.

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

Comments

0

From the Docs:

Why mixing interpolation and expressions is bad practice:

  • It increases the complexity of the markup
  • There is no guarantee that it works for every directive, because interpolation itself is a directive. If another directive accesses attribute data before interpolation has run, it will get the raw interpolation markup and not data.
  • It impacts performance, as interpolation adds another watcher to the scope.
  • Since this is not recommended usage, we do not test for this, and changes to AngularJS core may break your code.

— AngularJS Developer Guide - mixing interpolation and expressions

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.