Ahmed Alsahli Ahmed Alsahli - 5 months ago 22
AngularJS Question

Represent ng-model in a controller

I am developing application using AngularJS and ASP.Net MVC...

In my view, I have used

ng-repeat=u in users
in a
div
. Inside it, I set the
ng-model
in a textbox as
ng-model=u.fName
where
fName
is database field.

My question is, how I can represent the
ng-model=u.fName
in the angular controller??

For example, I tried to use
$scope.n.fName
but it doesn't work.

Answer

Note that u is only existing in ng-repeat block. If you want to show/access to value changed by textbox outside of block, you should access via users again with another ng-repeat block:

<li ng-repeat="u in users">{{u.fName}}</li>

Or add ng-change="change(u)" to your textbox to get u anytime it's changed.

<input ng-model="u.fName" ng-change="change(u)" />

Plunkr example