AngularJS 错误:fnPtr 不是函数 [英] AngularJS error: fnPtr is not a function

查看:22
本文介绍了AngularJS 错误:fnPtr 不是函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个示例 AngularJS 和 SpringMVC 项目.spring 方法工作正常,但我的站点控制器中的函数声明有问题.我的应用程序应该从文本输入中返回一个单词,但是当我单击按钮时,出现此错误:

[13:23:58.900] "错误:fnPtr 不是函数解析器/_functionCall/<@http://localhost:8080/example/resources/js/Angular/angular.js:6542ngEventDirectives[directiveName]</</</<@http://localhost:8080/example/resources/js/Angular/angular.js:13256Scope.prototype.$eval@http://localhost:8080/example/resources/js/Angular/angular.js:8218Scope.prototype.$apply@http://localhost:8080/example/resources/js/Angular/angular.js:8298ngEventDirectives[directiveName]</</<@http://localhost:8080/example/resources/js/Angular/angular.js:13255createEventHandler/eventHandler/<@http://localhost:8080/example/resources/js/Angular/angular.js:2095forEach@http://localhost:8080/example/resources/js/Angular/angular.js:130createEventHandler/eventHandler@http://localhost:8080/example/resources/js/Angular/angular.js:2094"

这是我的 index.html:

<html lang="en" ng-app="Apken"><头><meta charset="UTF-8"><title>在此处插入标题</title><script src="resources/js/Angular/angular.js"></script><script src="resources/js/controler.js"></script><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()">点击!</button>

<ul><li ng-repeat="name in names">{{name}}</li></html>

和controler.js:

function theNamer ($scope,$http){$scope.myName='aa';$scope.fetchList=新函数(){$http.get('ca/list.json').success(function(thList){$scope.names = thList;});}$scope.send=新函数(){$http.post('ca/set/3').success(function(){$scope.fetchList;});}$scope.fetchList;}var Apken = angular.module('Apken',[]);Apken.controller('theNamer', theNamer);

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

解决方案

我已经测试了你的代码.使用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();}

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:

[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:

<!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:

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.

解决方案

I have tested your code. Using AngularJS 1.0.7, the error disappears when you replace

$scope.send = new function() {

with

$scope.send = function () {

and same applies to fetchList.

I guess you mixed the two syntaxes function(*args*) { *body* } and new Function(*args*, *body*). Check on MDN: Function.

You have also to change your code in order to get your fetchList properly called:

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();

}

这篇关于AngularJS 错误:fnPtr 不是函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆