0

I am trying to load a local json file and get the data in to a $scope variable.

myApp.controller('myController', ['$scope', '$http', function ($scope, $http) {

 $scope.menu = function () {
        $http.get('file.json')
            .then(function (data) {
                          $scope.menu=data.data.menu;  

            });
  };

Also I tried return data.data.menu. But not menu is not updating.

Tried caling this on page load.

$scope.menu();

In html:

<body ng-app="myApp">
    <div ng-controller="myController">
    <ul class="nav navbar-nav" ng-repeat="item in menu">
    // other stuffs

What is wrong here.

I Simply put the below code directly in the controller, but still not working.

 $http.get('file.json')
            .then(function (data) {
                          $scope.menu=data.data.menu;  

            });
4
  • Are you sure there is not an error? What is data? Commented Nov 24, 2017 at 16:31
  • I can see the data while debugging Commented Nov 24, 2017 at 16:36
  • stackoverflow.com/questions/16930473/… check these answers probably same issue Commented Nov 24, 2017 at 16:36
  • The $scope propery with the name menu is overloaded. The code uses it both as the name of a function and as the name of data for the View. Commented Nov 24, 2017 at 21:04

1 Answer 1

1

Try this:

myApp.controller('myController', [
    '$scope',
    '$http',
    function(
        $scope,
        $http
    )
    {
        $scope.menu = [];

        $scope.getItems = function()
        {
            $http.get('file.json')
            .then(function(response)
            {
                $scope.menu = response.data.menu;
            });
        };

        $scope.getItems();
    }
]);

Also, you need to make sure that file.json is served as a static file from the web server and has the following structure:

{
    "menu": []
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.