gpt4 book ai didi

javascript - AngularJS undefined 不是一个函数

转载 作者:可可西里 更新时间:2023-11-01 16:38:26 25 4
gpt4 key购买 nike

我正在使用 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/

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