简体   繁体   中英

Angularjs fails http get request

I am trying some angularjs tutorials and dont know why the dropdown does not populate.

I get an error in angular.js file

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

below is my code.

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

})();

You have switched the order of the variables in the controller:

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

Switch to:

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

It might help you!!! 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>

First of all, the order of injections you make is not right. It must be fixed, as shown below:

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

('ctrlName', ['A', 'B', function(A, B) ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM