TypeError:string不是一个函数Angularjs,PHP

时间:2014-04-01 03:04:46

标签: javascript angularjs

以下php第一次运行。虽然在对PHP文件进行一次或两次往返之后,javascript控制台会抛出错误。一旦错误被抛出,应用程序就会停止一起工作。

TypeError: string is not a function
at http://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js:169:393
at http://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js:186:390
at h.$eval (http://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js:108:40)
at h.$apply (http://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js:108:318)
at HTMLInputElement.<anonymous> (http://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js:186:372)
at http://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js:30:104
at r (http://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js:7:386)
at HTMLInputElement.c  
(http://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js:30:86) angular.js:9563
(anonymous function) angular.js:9563
(anonymous function) angular.js:7004
h.$apply angular.js:12275
(anonymous function) angular.js:18332
(anonymous function) angular.js:2716
r angular.js:330

HTML:

<body>
    <div class="container" ng-controller="case4Ctrl">
        <div class="row">
            <h1 class="text-center">Enter a number to see if it is prime</h1>
            <input type="text" ng-model="number" value="" placeholder="" ng-keyup="isprime()">
            {{isprime}}
        </div>

    </div>
    <script>
     var app = angular.module('case4', []);

     app.controller('case4Ctrl', ['$scope', '$http', function ($scope, $http) {
        $scope.isprime = function(){
            console.log($scope.number);
            if($scope.number){
                console.log("test");
                $http.get("./isPrime.php?number=" + $scope.number).success(function(data){
                    $scope.isprime = data;
                });
            }
        };
     }])

    </script>
    <!-- jQuery -->
    <script src="//code.jquery.com/jquery.js"></script>
    <!-- Bootstrap JavaScript -->
    <script src="//netdna.bootstrapcdn.com/bootstrap/3.1.0/js/bootstrap.min.js"></script>
</body>

PHP:

<?php 
$number = $_GET['number'];


function isPrime($param){
    if ($param == 1){ return "1 is never a prime number";}
    if ($param == 2){ return "2 is the only even prime number";}

    for ($i = 3; $i < $param; $i += 2)  {
       if (($param % $i) == 0){ return $param. " is not a prime number"; }
    }

    return $param ." is a prime number";

}

echo isPrime($number);


?>

1 个答案:

答案 0 :(得分:0)

ng-keyup="isprime()"

然后在你的代码中写下:

 $scope.isprime = data;

显然不会起作用,数据不是函数,而是服务器返回的字符串。 它在服务器之前工作,因为isprime函数尚未被字符串覆盖。

将keyup处理程序更改为

ng-keyup="getIsPrime()"

和相应的函数名称

 $scope.getIsPrime=function(){...}