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;  

            });
Nidheesh
  • 4,390
  • 29
  • 87
  • 150

1 Answers1

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": []
}
Marcus
  • 321
  • 4
  • 10