繁体   English   中英

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

[英]How do I get a value from an Angular repeat loop into a button's onclick method?

我目前正在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部分是错误的。 我想知道我想做的事是否有可能以一种干净的方式进行。

使用ngClick代替onClick

这样尝试

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

控制者

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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