AngularJS错误:fnPtr不是函数

e j*_*jay 19 html javascript spring angularjs

我正在尝试编写一个示例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
"
Run Code Online (Sandbox Code Playgroud)

这是我的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>
Run Code Online (Sandbox Code Playgroud)

和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);
Run Code Online (Sandbox Code Playgroud)

我注意到,这必须是ng-click值中函数声明的某种问题.在现场启动controler.js工作正常,但它崩溃,当我点击按钮.

6fo*_*der 41

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

$scope.addTask = {};

$scope.addTask = function() {};
Run Code Online (Sandbox Code Playgroud)

  • 我得到了那个错误,因为我在foo()中编写了`ng-repeat ="f"而不是`f中的`ng-repeat ="f". (2认同)

Pie*_*ica 5

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

$scope.send = new function() {
Run Code Online (Sandbox Code Playgroud)

$scope.send = function () {
Run Code Online (Sandbox Code Playgroud)

同样适用于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();

}
Run Code Online (Sandbox Code Playgroud)