19

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.

2 Answers 2

41

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:

$scope.addTask = {};

$scope.addTask = function() {};
Sign up to request clarification or add additional context in comments.

4 Comments

I got that error, because I wrote ng-repeat="f in foo()" instead of ng-repeat="f in foo".
@6footunder, fantastic, it's nice catch, I wasted 3 hours looking for this problem. Function Name and relative Variable name should not be same.
i got it because i named a function the same as the controller ControllerAs syntax
It also occurred to me because i used the same name in form name and in form submit. So indeed, nice catch!
5

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

}

4 Comments

The error has been disappeared, but there is another problem: the fetchList function doesn't want to work. When I've keep the "new" declaration: $scope.fetchList = new function() it works only, when I refresh the browser.
@ejay you must also add () when you call it, like this: $scope.fetchList();. I'll update the answer with the correct code.
My fault, I forgot aboout it. Thanks!
@ejay no problem. If everything works can you please mark the answer as correct? :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.