将 table 中的唯一值绑定到复选框的 ng-model
Binding an unique value from a table to ng-model of a check box
我有一个 table(其中填充了使用 ng-repeat 从后端接收的数据。)每一行都有一个复选框,如果选中了该复选框(多个)我想向服务器发送请求以删除 rows.To 执行此操作 我想将复选框绑定到 table 中的唯一字段,但无法获得正确的语法。
我尝试了我在 SO 中阅读的以下选项,但似乎没有任何效果。
<input type="checkbox" ng-model="demo.Custid" >//this replaced the value in the table with true or false.
<input type="checkbox" ng-model="formData.checkboxes[demo.Custid]" >//this gave me values in the below format {"12500":true,"125001":false}
我是否可以获取直接绑定到 ng-model 的客户 ID 的值(在本例中)?我想获取这些值并向服务器发送请求以从table对应客户。
.controller('Controller1',function($scope,$rootScope,$routeParams){
$scope.name = 'Controller1';
$scope.params = $routeParams;
$scope.formData = {
checkboxes: {}
};
$scope.demos = [
{ 'Account':'abc.com',
'Custid': 125000,
},
{'Account':'abc.com',
'Custid': 125001, },
{ 'Account':'abc.com',
'Custid': 125002,}
]
})
我是 angular js 的新手,任何 help/pointers 都会很有帮助。
使用 ng-click。
您已经有一个 ng-repeat 循环演示。看到 $scope.demos 和 demo.Custid,我想你刚刚做了
ng-repeat="demo in demos".
现在只需将 demo.Custid 传递给作用域上定义的函数,使用
ng-点击="myfunction(demo.Custid)"
我对您的代码进行了一些调整以方便我自己使用,但这应该很明显:
编辑: 我刚刚意识到我在 jsfiddle 中使用 angular 1.01 模板来创建此代码,我将保留它,但这里有一个 fiddle 到 angularjs 1.5.5
中的工作示例
index.html
<div ng-controller="Controller1">
<input type="submit" ng-click="sendToBackend()">
{{name}}
<table>
<thead><tr><td>Select</td><td>Customer</td></tr>
</thead>
<tbody>
<tr ng-repeat="demo in demos">
<td><input type="checkbox" ng-click="addToList(demo.Custid)"></td>
<td>{{demo.Custid}}</td>
</tr>
</tbody>
</table>
<ul>
<li ng-repeat="check in checkboxes">{{check}}</li>
</ul>
</div>
myapp.js
var myApp = angular.module('myApp', []);
function Controller1($scope) {
$scope.name = 'Controller1';
$scope.checkboxes = [];
$scope.demos = [{Account: 'abc.com',Custid: 125000,},
{Account: 'abc.com',Custid: 125001,},
{Account: 'abc.com',Custid: 125002,}];
$scope.addToList = function(id) {
$scope.checkboxes.push(id);
};
$scope.sendToBackend = function() {
alert("sending " + $scope.checkboxes.length + " items to backend");
};
};
从这里开始,您应该可以用它做任何您想做的事情。如果您有更具体的问题,请拍!
我有一个 table(其中填充了使用 ng-repeat 从后端接收的数据。)每一行都有一个复选框,如果选中了该复选框(多个)我想向服务器发送请求以删除 rows.To 执行此操作 我想将复选框绑定到 table 中的唯一字段,但无法获得正确的语法。 我尝试了我在 SO 中阅读的以下选项,但似乎没有任何效果。
<input type="checkbox" ng-model="demo.Custid" >//this replaced the value in the table with true or false.
<input type="checkbox" ng-model="formData.checkboxes[demo.Custid]" >//this gave me values in the below format {"12500":true,"125001":false}
我是否可以获取直接绑定到 ng-model 的客户 ID 的值(在本例中)?我想获取这些值并向服务器发送请求以从table对应客户。
.controller('Controller1',function($scope,$rootScope,$routeParams){
$scope.name = 'Controller1';
$scope.params = $routeParams;
$scope.formData = {
checkboxes: {}
};
$scope.demos = [
{ 'Account':'abc.com',
'Custid': 125000,
},
{'Account':'abc.com',
'Custid': 125001, },
{ 'Account':'abc.com',
'Custid': 125002,}
]
})
我是 angular js 的新手,任何 help/pointers 都会很有帮助。
使用 ng-click。
您已经有一个 ng-repeat 循环演示。看到 $scope.demos 和 demo.Custid,我想你刚刚做了
ng-repeat="demo in demos".
现在只需将 demo.Custid 传递给作用域上定义的函数,使用 ng-点击="myfunction(demo.Custid)"
我对您的代码进行了一些调整以方便我自己使用,但这应该很明显:
编辑: 我刚刚意识到我在 jsfiddle 中使用 angular 1.01 模板来创建此代码,我将保留它,但这里有一个 fiddle 到 angularjs 1.5.5
中的工作示例index.html
<div ng-controller="Controller1">
<input type="submit" ng-click="sendToBackend()">
{{name}}
<table>
<thead><tr><td>Select</td><td>Customer</td></tr>
</thead>
<tbody>
<tr ng-repeat="demo in demos">
<td><input type="checkbox" ng-click="addToList(demo.Custid)"></td>
<td>{{demo.Custid}}</td>
</tr>
</tbody>
</table>
<ul>
<li ng-repeat="check in checkboxes">{{check}}</li>
</ul>
</div>
myapp.js
var myApp = angular.module('myApp', []);
function Controller1($scope) {
$scope.name = 'Controller1';
$scope.checkboxes = [];
$scope.demos = [{Account: 'abc.com',Custid: 125000,},
{Account: 'abc.com',Custid: 125001,},
{Account: 'abc.com',Custid: 125002,}];
$scope.addToList = function(id) {
$scope.checkboxes.push(id);
};
$scope.sendToBackend = function() {
alert("sending " + $scope.checkboxes.length + " items to backend");
};
};
从这里开始,您应该可以用它做任何您想做的事情。如果您有更具体的问题,请拍!