I'm trying to show some data on screen using AngularJS, ASP.NET and JSON.NET. When I'm calling $.getJSON it returns a normal JSONArray, but when I'm putting the data in a this.variablename inside my Angular controller, the data changes and Angular won't display it in my view.
Model
public class Directory
{
public int Id { get; set; }
public string Name { get; set; }
public List<Note> Notes { get; set; }
public int ParentId { get; set; }
public List<int> DirectoryIds { get; set; }
public virtual Directory Parent { get; set; }
public virtual ICollection<Directory> Directories { get; set; }
}
.NET controller action
public string GetDirectories()
{
var directories = db.Directories.Where(d => d.ParentId.Equals(0)).ToList();
return JsonConvert.SerializeObject(new { directorylist = directories }, Formatting.Indented,
new JsonSerializerSettings
{
ReferenceLoopHandling = ReferenceLoopHandling.Ignore
});
}
Angular JS
(function () {
var app = angular.module('notes', []);
app.controller('DirectoryController', function () {
this.directories = directories; //Check output 1
});
var directories = $.getJSON("/Directory/GetDirectories", function (data) {
return data; //Check output 2
});
})();
Output 1 (larger image http://i62.tinypic.com/2ppd0kg.png)

Output 2

$http.get()function instead of using a jquery function; aside from that, both frameworks implement the get function asynchronously, so you should set your data in the success function, instead of assigning it to the function itself.