如何从Angular重复循环中获取值到按钮的onclick方法?

时间:2015-08-11 20:39:15

标签: javascript angularjs

我目前正在开发一个Angular项目,我需要获取存储在控制器中的项目的ID。我正在使用此代码将数据打印到表中(编辑以查看问题区域:

    <div ng-app="application" ng-controller="services_controller">
        <table>
            ...
            <tr ng-repeat="x in environment_service_packages | orderBy:'environment'">
                <td>{{x.id}}</td>
                <td><input type="button" onclick="ping({{x.id}});" value="Ping"></td>
                ...
            </tr>
        </table>
    </div>

我收到以下错误:

Error: [$compile:nodomevents] http://errors.angularjs.org/1.3.14/$compile/nodomevents
    at Error (native)
    at https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js:6:417
    at $get.c.push.compile.pre (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js:68:382)
    at Z (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js:70:149)
    at A (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js:59:14)
    at g (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js:51:299)
    at g (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js:51:316)
    at g (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js:51:316)
    at https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js:50:415
    at $get.h (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js:52:283) <input type="button" onclick="ping({{x.id}});" value="Ping">

显然,onclick部分是错误的。我想知道我正在尝试以干净的方式做什么。

1 个答案:

答案 0 :(得分:3)

使用ngClick代替onClick

试试这个

<input type="button" ng-click="ping(x);" value="Ping">

<强>控制器

$scope.ping=function(x){
  console.log(x.id);
}