19

我正在尝试编写一个示例 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 工作正常,但是当我单击按钮时它崩溃了。

4

2 回答 2

41

只是想为收到此错误的任何人添加,也可以看出您是否像我一样犯了创建与函数同名的变量的 n00b 错误(从 ng-click 调用的函数:

$scope.addTask = {};

$scope.addTask = function() {};
于 2014-01-19T04:31:28.270 回答
5

我已经测试了你的代码。使用AngularJS 1.0.7,替换时错误消失

$scope.send = new function() {

$scope.send = function () {

同样适用于fetchList.

我猜你混合了这两种语法function(*args*) { *body* }new Function(*args*, *body*). 检查 MDN:功能

您还必须更改代码才能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();

}
于 2013-10-06T11:58:34.070 回答