admin管理员组文章数量:1025217
(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
};
}
(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
};
}
Share
Improve this question
edited Feb 2, 2015 at 9:49
Almin Islamovic
asked Feb 2, 2015 at 9:22
Almin IslamovicAlmin Islamovic
2781 gold badge5 silver badges13 bronze badges
8
- if you do a console.log(MainController); just before the last line, what do you get? – satchcoder Commented Feb 2, 2015 at 9:32
- If you ask this kind of question - you should not use beta version of the framework. Stay with stable one. – lujcon Commented Feb 2, 2015 at 9:50
- Could you provide a jsfiddle or something ? – Bongo Commented Feb 2, 2015 at 9:52
- @satchcoder if i do that, i get the function until the last line, – Almin Islamovic Commented Feb 2, 2015 at 9:53
- @lujcon Tried it.. doesn't work either.. – Almin Islamovic Commented Feb 2, 2015 at 9:53
4 Answers
Reset to default 3Fixed 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./books/v1/volumes?q=isbn:0747532699")
.success(function (response) {
console.log(response);
$scope.sport = response.items;
});
}]);
Update
Plunker version for AngularJS v1.3.x
Order matters :-
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'];
Here is Working Fiddle, Its just a basic because I guess you have problem with finding your controller... I hope it helps you link
(function(){
var app = angular.module("sports",[]);
app.controller("MainController", function($scope){
this.msg = 'Hello World';
});
})();
I guess you have have messed up with closure (Brackets defining self invoking functions in JS. ), which I have corrected.
And do follow definition structure proposed by Angular Docs.
For angularjs v1.4.x the success
and error
methods are now deprecated
// 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()
method is the replacement for deprecated method success()
Github Reference link
API Reference link
(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
};
}
(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
};
}
Share
Improve this question
edited Feb 2, 2015 at 9:49
Almin Islamovic
asked Feb 2, 2015 at 9:22
Almin IslamovicAlmin Islamovic
2781 gold badge5 silver badges13 bronze badges
8
- if you do a console.log(MainController); just before the last line, what do you get? – satchcoder Commented Feb 2, 2015 at 9:32
- If you ask this kind of question - you should not use beta version of the framework. Stay with stable one. – lujcon Commented Feb 2, 2015 at 9:50
- Could you provide a jsfiddle or something ? – Bongo Commented Feb 2, 2015 at 9:52
- @satchcoder if i do that, i get the function until the last line, – Almin Islamovic Commented Feb 2, 2015 at 9:53
- @lujcon Tried it.. doesn't work either.. – Almin Islamovic Commented Feb 2, 2015 at 9:53
4 Answers
Reset to default 3Fixed 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./books/v1/volumes?q=isbn:0747532699")
.success(function (response) {
console.log(response);
$scope.sport = response.items;
});
}]);
Update
Plunker version for AngularJS v1.3.x
Order matters :-
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'];
Here is Working Fiddle, Its just a basic because I guess you have problem with finding your controller... I hope it helps you link
(function(){
var app = angular.module("sports",[]);
app.controller("MainController", function($scope){
this.msg = 'Hello World';
});
})();
I guess you have have messed up with closure (Brackets defining self invoking functions in JS. ), which I have corrected.
And do follow definition structure proposed by Angular Docs.
For angularjs v1.4.x the success
and error
methods are now deprecated
// 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()
method is the replacement for deprecated method success()
Github Reference link
API Reference link
本文标签:
版权声明:本文标题:javascript - angularJS 1.4.0 Argument 'MainController' is not a function, got undefined - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1741939897a1896488.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论