简体   繁体   中英

AngularJS Uncaught ReferenceError: ng-repeat ng-click

I do not know why an error. When you run the ng-click in the ng-repaet, Uncaught ReferenceError occurs. Are you sure you will not know the cause of the click event does not occur?

test.js

var MyApp = angular.module('MyApp', []);

MyApp.controller('MyCtrl', ['$scope', function ($scope) {
    $scope.add = function() {
        alert("add click");
    };

    $scope.list = function() {
        return [{"1": {id: 1, name: "aaa"},
                "2": {id: 2, name: "bbb"},
                "3": {id: 3, name: "ccc"}}];
    }
}]);

test.html

<html>
<head>
    <meta charset="utf-8">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css">
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.1/angular.min.js"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
    <script type="text/javascript" src="test.js"></script>
    <title>Sample</title>
</head>
<body ng-app="MyApp">
    <div class="container" ng-controller="MyCtrl">
        <table class="table table-striped" ng-init="list = list()">
            <tr ng-repeat="element in list">
                <td ng-repeat="nake in element" onclick="add()">{{nake.id}}</td>
            </tr>
        </table>
    </div>
</body>
</html>

Error

Uncaught ReferenceError: add is not defined 

You should use the ng-click angular tag, like this:

<td ng-repeat="nake in element" ng-click="add(nake.name)">{{nake.id}}</td>

Check this fiddle to see that it works properly.

您需要使用ng-click指令而不是onclick

使用ng-click代替onclick,因为绑定到范围的任何函数都无法通过onclick事件访问。

<td ng-repeat="nake in element" ng-click="add()">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM