Set angular scope variable in markup

Nix picture Nix · May 28, 2013 · Viewed 189.3k times · Source

Simple question: How can I set a scope value in html, to be read by my controller?

JSFiddle: http://jsfiddle.net/ncapito/YdQcX/

Answer

Glogo picture Glogo · Feb 10, 2015

ng-init does not work when you are assigning variables inside loop. Use {{myVariable=whatever;""}}

The trailing "" stops the Angular expression being evaluated to any text.

Then you can simply call {{myVariable}} to output your variable value.

I found this very useful when iterating multiple nested arrays and I wanted to keep my current iteration info in one variable instead of querying it multiple times.