angularJS 1.4.0 参数 'MainController' 不是函数,未定义
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]);
}());
所以是的,这个脚本不工作,得到错误它找不到 "main controller as 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
};
}
订单事项:-
app.controller("MainController", MainController);
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);
});
};
MainController.$inject = ['$scope','$http'];
这是工作 Fiddle,它只是一个基本的,因为我猜你在找到你的控制器时遇到了问题...我希望它能帮助你
link
(function(){
var app = angular.module("sports",[]);
app.controller("MainController", function($scope){
this.msg = 'Hello World';
});
})();
我猜你搞砸了闭包(Brackets defining self-invoking functions in JS.),我已经更正了。
并遵循 Angular 文档提出的定义结构。
修复了你 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;
});
}]);
更新
对于 angularjs v1.4.x success
和 error
方法现已弃用
// Simple GET request example :
$http.get('/someUrl').
then(function(response) {
// this callback will be called asynchronously
// when the response is available
}, function(response) {
// called asynchronously if an error occurs
// or server returns response with an error status.
});
then()
方法是已弃用方法 success()
的替代方法
(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]);
}());
所以是的,这个脚本不工作,得到错误它找不到 "main controller as 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
};
}
订单事项:-
app.controller("MainController", MainController);
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);
});
};
MainController.$inject = ['$scope','$http'];
这是工作 Fiddle,它只是一个基本的,因为我猜你在找到你的控制器时遇到了问题...我希望它能帮助你 link
(function(){
var app = angular.module("sports",[]);
app.controller("MainController", function($scope){
this.msg = 'Hello World';
});
})();
我猜你搞砸了闭包(Brackets defining self-invoking functions in JS.),我已经更正了。
并遵循 Angular 文档提出的定义结构。
修复了你 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;
});
}]);
更新
对于 angularjs v1.4.x success
和 error
方法现已弃用
// Simple GET request example :
$http.get('/someUrl').
then(function(response) {
// this callback will be called asynchronously
// when the response is available
}, function(response) {
// called asynchronously if an error occurs
// or server returns response with an error status.
});
then()
方法是已弃用方法 success()