我正在使用 Angularjs 尝试一个简单的 http 应用程序。 我的代码是
var studentApp = angular.module('studentApp',['ui.bootstrap']);
studentApp.factory('studentSession', function($http){
return {
getSessions: function() {
return $http.post('/services', {
type : 'getSource',
ID : 'TP001'
});
}
}
});
studentApp.controller('studentMenu',function($scope, studentSession){
studentSession.getSessions().success($scope.handleSuccess);
$scope.handleSuccess = function(data, status) {
console.log(data);
};
});
在尝试运行上面的代码时,我在 chrome 中收到错误 Undefined is not a function
。通过 stackoverflow 搜索推荐的修复是提供功能范围。即使在确定函数范围后错误仍然存在。
怀疑 Angular 文件有问题,因为如果我将 getSession 代码移到 Controller 内,代码工作正常。但错误仍然存在于 Angular 版本 1.0.6 和 1.1.4(未压缩和缩小版本)。
错误在 firefox 中有所不同。它的 fn 不是函数
未压缩的 angularjs 和 b 不是函数
缩小的 angularjs。
关于如何解决这个问题有什么建议吗?
最佳答案
Oki 所以基于评论下面的代码可以工作
var studentApp = angular.module('studentApp',['ui.bootstrap']);
studentApp.factory('studentSession', function($http){
return {
getSessions: function() {
return $http.post('/services', {
type : 'getSource',
ID : 'TP001'
});
}
}
});
studentApp.controller('studentMenu',function($scope, studentSession){
//------Code Change---------
//Moved before function call.
$scope.handleSuccess = function(data, status) {
console.log(data);
};
studentSession.getSessions().success($scope.handleSuccess);
});
关于javascript - AngularJS undefined 不是一个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16271821/