javascript AngularJS 错误:fnPtr 不是函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19208518/
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 error: fnPtr is not a function
提问by e jay
I'm trying to write a sample AngularJS, and SpringMVC project. The spring methods works fine, but I have a problem with declaraton of function in my site controller. My app should return a word from text input, but when I click the button, I've got this error:
我正在尝试编写一个示例 AngularJS 和 SpringMVC 项目。spring 方法工作正常,但我的站点控制器中的函数声明有问题。我的应用程序应该从文本输入中返回一个单词,但是当我单击按钮时,出现以下错误:
[13:23:58.900] "Error: fnPtr is not a function
parser/_functionCall/<@http://localhost:8080/example/resources/js/Angular/angular.js:6542
ngEventDirectives[directiveName]</</</<@http://localhost:8080/example/resources/js/Angular/angular.js:13256
Scope.prototype.$eval@http://localhost:8080/example/resources/js/Angular/angular.js:8218
Scope.prototype.$apply@http://localhost:8080/example/resources/js/Angular/angular.js:8298
ngEventDirectives[directiveName]</</<@http://localhost:8080/example/resources/js/Angular/angular.js:13255
createEventHandler/eventHandler/<@http://localhost:8080/example/resources/js/Angular/angular.js:2095
forEach@http://localhost:8080/example/resources/js/Angular/angular.js:130
createEventHandler/eventHandler@http://localhost:8080/example/resources/js/Angular/angular.js:2094
"
This is my index.html:
这是我的 index.html:
<!DOCTYPE html>
<html lang="en" ng-app="Apken">
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script src="resources/js/Angular/angular.js"></script>
<script src="resources/js/controler.js"></script>
</head>
<body ng-controller="theNamer">
<div class="input-append">
<input style="width:358px;" class="span2" type="text" ng-model="myName" required min="1" />
<button class="btn btn-primary" ng-disabled="!myName" ng-click="send()">Click!</button>
</div>
<ul>
<li ng-repeat="name in names">{{name}}</li>
</ul>
</body>
</html>
And controler.js:
和controler.js:
function theNamer ($scope,$http){
$scope.myName='aa';
$scope.fetchList=new function()
{
$http.get('ca/list.json').success(function(thList){
$scope.names = thList;
});
}
$scope.send=new function()
{
$http.post('ca/set/3').success(function(){
$scope.fetchList;
});
}
$scope.fetchList;
}
var Apken = angular.module('Apken',[]);
Apken.controller('theNamer', theNamer);
I've noticed, that must be a some kind of problem with function declaration in the ng-click value. On site startup controler.js works fine, but it crashes, when I click the button.
我注意到,这一定是 ng-click 值中的函数声明存在某种问题。现场启动 controler.js 工作正常,但当我单击按钮时它崩溃了。
采纳答案by Spak
I have tested your code. Using AngularJS 1.0.7, the error disappears when you replace
我已经测试了你的代码。使用 AngularJS 1.0.7,替换时错误消失
$scope.send = new function() {
with
和
$scope.send = function () {
and same applies to fetchList
.
同样适用于fetchList
.
I guess you mixed the two syntaxes function(*args*) { *body* }
and new Function(*args*, *body*)
. Check on MDN: Function.
我猜你混合了两种语法function(*args*) { *body* }
和new Function(*args*, *body*)
. 检查 MDN:功能。
You have also to change your code in order to get your fetchList
properly called:
您还必须更改代码才能fetchList
正确调用:
function theNamer($scope, $http) {
$scope.myName = 'aa';
$scope.fetchList = function() {
$http.get('ca/list.json').success(function(thList) {
$scope.names = thList;
});
};
$scope.send = function() {
$http.post('ca/set/3').success(function() {
$scope.fetchList();
});
};
$scope.fetchList();
}
回答by 6footunder
Just wanted to add for anybody receiving this error, it can also be seen if you, like me, make the n00b mistake of creating a variable with the same name as function (the function being called from ng-click:
只是想为收到此错误的任何人添加,如果您像我一样犯了 n00b 错误,即创建与函数同名的变量(从 ng-click 调用的函数:
$scope.addTask = {};
$scope.addTask = function() {};