Angularjs未通过http get请求

时间:2015-02-14 09:11:47

标签: javascript angularjs

我正在尝试一些angularjs教程,并且不知道为什么下拉列表没有填充。

我在angular.js文件中收到错误

TypeError: undefined is not a function. 
if ($rootScope.$broadcast('$locationChangeStart', newUrl,
                                    oldUrl).defaultPrevented) {

下面是我的代码。

<!DOCTYPE html>
<html>
<head>
<link rel="" type="text/css" href="bootstrap.min.js" />
</head>
<body ng-app = "subtitle" ng-controller="loginController">
<script type="text/javascript" src="angular.min.js"></script>
<script type="text/javascript" src="app.js"></script> 
<select ng-model="collections" ng-options="item.Id as item.Name for item in languages">
    <option value="">Select Account</option>
</select>
</body>
</html>

(function(){

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

app.controller('loginController', ['$http','$scope',function($scope,$http) {
$scope.collections = null;
$scope.languages = [];

$http.get('http://lapi.cd.com/masterdata?type=languages').success(function(data) {
$scope.languages = data;
});
}]);

})();

3 个答案:

答案 0 :(得分:3)

您已切换控制器中变量的顺序:

app.controller('loginController', ['$http','$scope',function($scope,$http) 

切换到:

app.controller('loginController', ['$http','$scope',function($http,$scope) 

答案 1 :(得分:0)

它可能对你有所帮助! plnker

<!DOCTYPE html>
<html>

<head>
  <link rel="stylesheet" href="http://getbootstrap.com/2.3.2/assets/css/bootstrap.css">
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.4/angular.js"></script>
  <script>
    var app = angular.module('subtitle', []);
    app.controller('loginController', ['$http', '$scope',
      function($http,$scope) {
        $scope.collections = null;
        $scope.languages = [];

        $http.get('http://lapi.cd.com/masterdata?type=languages').success(function(data) {
          $scope.languages = data;
        });
      }
    ]);
  </script>
</head>

<body ng-app="subtitle" ng-controller="loginController">
  <select ng-model="collections" ng-options="item.Id as item.Name for item in languages">
    <option value="">Select Account</option>
  </select>
</body>

</html>

答案 2 :(得分:0)

首先,你做的注射顺序是不对的。它必须是固定的,如下所示:

app.controller('loginController',['$ scope','$ http',函数($ scope,$ http){...}]);

('ctrlName',['A','B',函数(A,B)......