2014-04-01 70 views
0

下面的php第一次工作。雖然在對PHP文件進行一兩次往返之後,JavaScript控制檯會拋出一個錯誤。一旦錯誤被拋出,應用程序將一起停止工作。TypeError:字符串不是函數Angularjs,PHP

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

作爲一個初步的印象,看起來像共同順便你有'$ scope.isprime'的功能以及作爲來自'$ http'調用的響應數據。嘗試改變其中一個名稱,看看是否有所作爲。 – dmahapatro

回答

0
ng-keyup="isprime()" 

然後在你的代碼中寫:

$scope.isprime = data; 

它不會很明顯,數據不是函數,而是服務器返回的字符串。 它在旅行到服務器之前工作,因爲isprime函數尚未被字符串覆蓋。

變化KEYUP處理成

ng-keyup="getIsPrime()" 

和你的函數名稱將相應

$scope.getIsPrime=function(){...} 
相關問題