我正在尝试编写一个示例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
"
这是我的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>
和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);
我注意到,这必须是ng-click值中函数声明的某种问题。在网站上启动controler.js工作正常,但当我点击按钮时崩溃。
答案 0 :(得分:41)
只是想为任何收到此错误的人添加,也可以看到,如果你像我一样,犯了n00b错误,创建一个与函数同名的变量(从ng-click调用的函数:
$scope.addTask = {};
$scope.addTask = function() {};
答案 1 :(得分:5)
我已经测试了你的代码。使用AngularJS 1.0.7时,更换
时错误消失$scope.send = new function() {
与
$scope.send = function () {
同样适用于fetchList
。
我猜您混合了两种语法function(*args*) { *body* }
和new Function(*args*, *body*)
。检查MDN:Function。
您还需要更改代码才能正确调用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();
}