2014-07-07 62 views
2

我是Angular.js的新成員,我正在關注角度網站的教程。

我想從$ http服務,從json文件中獲取數據,但沒有任何反應! 甚至錯誤的狀態是不工作...

這裏是我的代碼:

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

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

和JS部分:

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; 
     });  

}]); 

解決方案:

謝謝sylwester,我發現了這個問題,但這真的很奇怪。所以,我只是說我的js文件,這樣的: (在開始和結束。)

(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; 
      }); 


     }]); 
})(); 
+0

你有'NG-控制器='MonitorCtrl''在'HTML'? –

回答

5

更有可能的是,你錯過了什麼,請參閱有: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; 
     }); 


}); 
+0

問題的解決方案! –