Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angularJS 1.4.0 Argument 'MainController' is not a function, got undefined

 (function () {

    var app = angular.module("Sports", []);
    var MainController = function($scope, $http) {

        var onUser = function (response) {
            obj = JSON.parse(response);
            $scope.sport = angular.fromJson(obj);
        };


        $http.get("/api/SportApi/Get").success(function (response) {
            obj = JSON.parse(response);
            $scope.sport = angular.fromJson(obj);
        });
    };
    app.controller("MainController", ["$scope", "$http", MainController]);
}());

So yeah, this script is not working, getting the error it can not find the "main controller as function" whats the problem?

EDIT: the error cause is in this function:

 function consoleLog(type) {
  var console = $window.console || {},
      logFn = console[type] || console.log || noop,
      hasApply = false;

  // Note: reading logFn.apply throws an error in IE11 in IE8 document mode.
  // The reason behind this is that console.log has type "object" in IE8...
  try {
    hasApply = !!logFn.apply;
  } catch (e) {}

  if (hasApply) {
    return function() {
      var args = [];
      forEach(arguments, function(arg) {
        args.push(formatError(arg));
      });
      return logFn.apply(console, args); //throws exception
    };
  }
like image 506
Almin Islamovic Avatar asked Sep 29 '22 20:09

Almin Islamovic


1 Answers

Fixed you fiddle. Possibly, problem is in immediate function. Also fixed ng-app and response processing

HTML

<div ng-app="Sports"> 
    <div ng-controller="MainController">
        <table class="table table-striped table-hover">
            <thead>Sport</thead>
            <tr ng-repeat="x in sport">
                {{sport}}
            </tr>
        </table>
    </div>
</div>

JS

angular
    .module("Sports", [])
    .controller("MainController", ["$scope", "$http", function($scope, $http) {
        $http.get("https://www.googleapis.com/books/v1/volumes?q=isbn:0747532699")
            .success(function (response) {
                console.log(response);
                $scope.sport = response.items;
            });
    }]);

Update

Plunker version for AngularJS v1.3.x

like image 180
oxfn Avatar answered Oct 06 '22 02:10

oxfn