I am very new to AngularJS, please accept my apology if this is an easy question. I also hope that the title of the question is understandable?
I am using AngularJS with ASP.Net MVC application to ready data from SQL server table and then display the data on the view. Everything looks fine but when I try to step forward for more advanced tasks, I find out that I am not able to read the object from Angular module.
For example, I want to read the contact details of a user, each user has different number of contacts. So, the ng-repear limitTo should not be the same. To do that, I wanted to read some data and then set a variable to the number of items that I found on the database and then use that variable in my ng-repear limitTo
This is part of my view:
<table>
<tr ng-repeat="contact in users | limitTo:loopVar">
<td>
<div ng-show="!contactsElements" class="item_list">{{contact.contacts + " (" + contact.contactDesc + ")"}}</div>
</td>
</tr>
Where loopView is the variable that I want to use
This is the Angular module/controller:
var myApp = angular.module('myApp', []);
myApp.controller('mainController', function ($scope, $http) {
$http.get('/Home/GetUser')
.success(function (result) {
$scope.users = result;
})
.error(function (data) {
console.log(data);
});
\\HOW I CAN SET THE loopVar HERE SO I CAN USE IT IN MY VIEW??
}
The MVC controller is below:
public JsonResult GetUser()
{
User userData = (User)Session["user"];
var db = new testDBEntities();
return this.Json((from userObj in db.Users
join uc in db.UserContacts
on userObj.Id equals uc.usrID
join us in db.Users
on userObj.usrSupervisor equals us.Id
where userObj.Id.Equals(userData.Id)
select new
{
Id = userObj.Id,
usrNme = userObj.usrNme,
fName = userObj.usrFirstName,
lName = userObj.usrLastName,
ssno = userObj.usrSSN,
contacts = uc.usrContact,
contactDesc = uc.usrContactDescription,
})
, JsonRequestBehavior.AllowGet
);
}
I hope you understand my point... In general, how I can read the returned JSON object in my Angular controller before using it in my view? As you can see, I am able to read data in my view by using ng-repeat directive but I don't know how to ready individual field in my Angular controller.
Thank you
loopVardefined? it doesn't appear to be a property of theusersarray coming from the server.....successcallback, the same way you assigned$scope.users. If you try to assign it outside the.success, the value won't exist yet because the response hasn't been returned by the server yet (async). you may want to read up on async if you are unsure. also, it is worth noting,.successis deprecated, and it is recommended that you use.theninstead.$scope.users=resultin the.success. And it is working properly... What if I want to read a specific peace of data? Such as the username$scope.username=result.usrNme... This is what I am looking to do with no luck so far.resultis a single object, then that should work correctly, assuming that it is inside the.success....