gpt4 book ai didi

javascript - angularJS 1.4.0 参数 'MainController' 不是函数,未定义

转载 作者:搜寻专家 更新时间:2023-11-01 04:16:31 27 4
gpt4 key购买 nike

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

是的,这个脚本不工作,出现错误,它找不到“作为函数的主 Controller ”,这是什么问题?

编辑:错误原因在这个函数中:

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

最佳答案

修复了你 fiddle .可能问题出在即时功能上。还修复了 ng-app 和响应处理

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

更新

Plunker version for AngularJS v1.3.x

关于javascript - angularJS 1.4.0 参数 'MainController' 不是函数,未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28274417/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com