javascript AngularJS:fn 不是函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30005409/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
AngularJS: fn is not a function
提问by mefe
After calling a function resultsI have an error: TypeError: fn is not a function. Before calling it, everything works fine, first $http.getgets good data. Any ideas how to fix it?
调用函数后,results我有一个错误:TypeError: fn is not a function. 在调用之前,一切正常,先$http.get获取好数据。任何想法如何解决它?
var app = angular.module("cowork", []);
app.config(function($interpolateProvider) {
$interpolateProvider.startSymbol('{[{');
$interpolateProvider.endSymbol('}]}');
});
app.controller("SearchDeskCtrl", ['$scope', '$http', function ($scope, $http) {
$scope.search = false;
$scope.city;
$http.get('/cowork/desks').success(function(data, status, headers, config) {
return $scope.results = data;
})
$scope.results = function(search) {
if (search){
$http.get('/cowork/desks_city', {city: $scope.city}).success(function(data, status, headers, config) {
search=false;
return data;
})}
}
}])
EDIT:My point in whole thing is to take results from first $http.getand then, after calling function, overwrite this data. Moreover, I need to pass one string parameter in second $http.getcall (in function) to pass cityparameter and then receive it in request(from Django side).
编辑:我的观点是首先获取结果$http.get,然后在调用函数后覆盖此数据。此外,我需要在第二次$http.get调用(在函数中)传递一个字符串参数来传递city参数,然后request(从 Django 端)接收它。
采纳答案by Pankaj Parkar
After getting a response from $http.get('/cowork/desks')ajax your $scope.resultsmethod is getting overridden with data
从$http.get('/cowork/desks')ajax得到响应后,您的$scope.results方法被数据覆盖
So while you are calling $scope.resultsmethod from it will throw because $scope.resultsno more available in controller scope context.
因此,当您$scope.results从它调用方法时会抛出,因为$scope.results在控制器范围上下文中不再可用。
TypeError: fn is not a function
类型错误:fn 不是函数
Fix would be you need to rename results of $http.getto something else like $scope.resultwill be fine.
修复是您需要将结果重命名$http.get为其他类似的东西$scope.result就可以了。
Code
代码
$http.get('/cowork/desks').success(function(data, status, headers, config) {
$scope.result = data;
})
回答by vinayakj
You are using results as a var in case 1 and as a function in case 2.
您在情况 1 中将结果用作 var,在情况 2 中用作函数。
Case1: return $scope.results = data;
Case2: $scope.results = function(search) {
Use appropriate names.
使用适当的名称。

