角度js控制器函数中未捕获的ReferenceError

时间:2016-01-06 12:49:15

标签: angularjs

当我调用在控制器中定义的函数时,函数未定义错误。

  

未捕获的ReferenceError:未定义deleteBook

<!DOCTYPE html>
<html>
<head>
  <meta charset='utf-8'>
  <title></title>
<body>

<div ng-app="myApp" ng-controller="customersCtrl"> 
<ul style="list-style:none;">
  <li ng-repeat="x in name">
    <a onclick="deleteBook({{ x.id }})" href="javascript:void(0);">{{ x.name + ' - ' + x.author }}</a>
  </li>
</ul>
</div>    
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script>
<script type="text/javascript">
    var app = angular.module('myApp', [] );
    app.controller('customersCtrl', function( $scope , $http ) {
        $scope.deleteBook = function ( pId ) {
            console.log( pId );
        }       
        $http.get("http://localhost/tangu/books.php")
        .then(
             function(response) {
                console.log(response);
                $scope.name = response.data;
             }
        );  
    });
</script>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

 <a onclick="deleteBook({{ x.id }})" href="javascript:void(0);">{{ x.name + ' - ' + x.author }}</a>

尝试使用此代替上面所写的内容。

 <a ng-click="deleteBook(x.id)"</a>

鉴于您的控制器中存在deleteBook功能。