2

I am new with Angular.js, and I am following the tutorial of the angular website.

I am trying to get my data from a json file with the $http service, but nothing happens!! Even the status of the error is not working...

Here is my code:

 <li>{{status}}</li>

 <ul class="messages">
      <li ng-repeat="message in messages">
            {{message.title}}
            <p>{{message.title2}} {{message.icon}}</p>    
      </li>
 </ul>

and the js part:

var monitorControllers = angular.module('monitorControllers', []);

monitorControllers.controller('MonitorCtrl', ['$scope', '$http',
    function($scope,$http) {
   $http.get('data/messages.json').success(function(data, status) {
       $scope.status = status;
       $scope.messages = data;
   }).error(function(data, status) {
           $scope.messages = data || "Request failed";
           $scope.status = status;
       });    

}]);

THE SOLUTION :

Thank you sylwester, I found the problem but it is really strange. So I just added in my js file, this: (at the beginning and the end..)

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

    monitorControllers.controller('MonitorCtrl', ['$scope', '$http',
        function ($scope, $http) {

            $http.get('data/messages.json').success(function(data, status) {
             $scope.status = status;
             $scope.messages = data;
             }).error(function(data, status) {
             $scope.messages = data || "Request failed";
             $scope.status = status;
             });


        }]);
})();
1
  • do you have ng-controller='MonitorCtrl' in your HTML ? Commented Jul 7, 2014 at 14:25

1 Answer 1

5

More likely you missed something please see there :http://jsbin.com/canuma/1/edit?html,js,output

html:

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.14/angular.min.js"></script>
<script src="http://code.jquery.com/jquery-2.1.0.min.js"></script>
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body ng-app="monitorControllers">
  <div ng-controller="MonitorCtrl">
   <li>{{status}} {{messages}}</li>

 <ul class="messages">
      <li ng-repeat="message in messages">
            {{message.title}}
            <p>{{message.title2}} {{message.icon}}</p>    
      </li>
 </ul>
      </div>
</body>
</html>

js:

var monitorControllers = angular.module('monitorControllers', []);

monitorControllers.controller('MonitorCtrl', function($scope, $http){

  $http.get('data/messages.json').success(function(data, status) {
       $scope.status = status;
       $scope.messages = data;
   }).error(function(data, status) {
           $scope.messages = data || "Request failed";
           $scope.status = status;
       });  


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

1 Comment

Solution in the question!

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.