0

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) The output that Angular makes of my JSON

Output 2

This is what I actually want to use but gets changed in output 1

1
  • 1
    you might consider switching to the angular resources or the angular $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. Commented Apr 15, 2015 at 11:37

2 Answers 2

2

As Claies commented and Protector one answered, the getJSON call was asynchronous and this way I couldn't fill the var directories before my page loads.

The answer was to use $http.get from within the controller and use a variable to store the context of the controller, which I have done with var cont = this. Then I needed to initialize my directories variable with an empty array to avoid errors on page load.

Then later on I just assign data from within the success Promise to my directories variable.

(function () {
    var app = angular.module('notes', []);

    app.controller('DirectoryController', ['$http', function ($http) {
        var cont = this; //Save the context
        cont.directories = []; //Empty array to avoid errors on page load

        $http.get('/Directory/GetDirectories').
          success(function (data, status, headers, config) {
              //Save the async data to directories in the right context
              cont.directories = data;
          }).
          error(function (data, status, headers, config) {
          });
    }]);
})();

Thanks for the help. Both the comment from @Claies and the answer from @Protector one helped me to find a fit answer myself.

If anyone has a better answer, feel free to post it, so I can mark it as answer.

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

Comments

1

getJSON asynchronously retrieves the data, and doesn't actually return it. You could try to set up your controller in the success parameter function:

var directories = $.getJSON("/Directory/GetDirectories", null, function (data) {
    app.controller('DirectoryController', function () {
        this.directories = data;
    });
});

Also note that the first parameter of getJSON is a data object that is sent to the server; in your example you provide it a javascript function.

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.