gpt4 book ai didi

javascript - Angular ng-repeat 动态

转载 作者:行者123 更新时间:2023-11-28 17:49:51 25 4
gpt4 key购买 nike

查看此plunker

在 jQuery 中,我可以获取其 td 的文本并将其放入警报中,但如何在 Angular 中制作它?我应该将其设为 JavaScript 原生吗?

这是脚本

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

app.controller('ctrl',function($scope){
$scope.edit = function(){
alert("ID = " + $scope.id + "\n NAME = " + $scope.name);
};
$scope.items = [
{id:"1",name:"name 1"},
{id:"2",name:"name 2"},
{id:"3",name:"name 3"}
];
});

HTML

  <body ng-controller="ctrl" ng-app="plunker">
<table>
<thead>
<tr>
<th>id</th>
<th>name</th>
<th>edit</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="x in items">
<td ng-model="x.id">{{x.id}}</td>
<td>{{x.name}}</td>
<td><button ng-click="edit()">edit</button></td>
</tr>
</tbody>
</table>
</body>

附注

ng-repeat 是动态的,所以我如何获取它的值?

最佳答案

只需将值作为参数传递给编辑函数即可,

 <td><button ng-click="edit(x)">edit</button></td>

然后函数将是,

$scope.edit = function(x){
console.log("Id is",x.id);
}

演示

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

app.controller('ctrl',function($scope){
$scope.edit = function(){
alert("ID = " + $scope.id + "\n NAME = " + $scope.name);
};
$scope.items = [
{id:"1",name:"name 1"},
{id:"2",name:"name 2"},
{id:"3",name:"name 3"}
];

$scope.edit = function(x){
console.log("$$Id is",x.id);
}
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-controller="ctrl" ng-app="plunker">
<table>
<thead>
<tr>
<th>id</th>
<th>name</th>
<th>edit</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="x in items">
<td ng-model="x.id">{{x.id}}</td>
<td>{{x.name}}</td>
<td><button ng-click="edit(x)">edit</button></td>
</tr>
</tbody>
</table>
</body>

关于javascript - Angular ng-repeat 动态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45808072/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com