使用 ng 选中的值添加到链接

Using the ng-checked value to add to a link

本文关键字:添加 链接 ng 使用      更新时间:2023-09-26

解释起来很复杂,但我会尽力而为。我想要实现的是,使用复选框值(这是我的 ID)并选中其中一个(有很多,因为它们是列表中的项目)时,我想使用该值并能够触发我的锚标签来编辑该项目。

复选框本身

<td><input type="checkbox" value="item.iid" ng-checked="item.iid" id="item_{{item.iid}}"></td>

编辑链接

<a href="#/edit/{{item.iid}}"><div class="edit">Edit</div></a>

我的编辑控制器

var EditCtrl = function ($scope, $location, $routeParams, Item) {
    $scope.action = "Update";
    var id = $routeParams.editId;
    $scope.item = Item.get({ id: id });
    $scope.save = function () {
        Item.update({ id: id }, $scope.item, function () {
            $location.path('/');
        });
    };
};
您应该

使用 ng-click 而不是 ng-checked,当用户单击时,您应该将当前项目标记为选中或未选中下面的示例

<a href="#/edit/{{lastChecked}}"><div class="edit">Edit</div></a>
<td><input type="checkbox" value="item.iid" ng-checked="amIChecked(item.iid)" ng-click="checkMe(item.iid)" id="item_{{item.iid}}"></td>

这是 js

var EditCtrl = function ($scope, $location, $routeParams, Item) {
    $scope.action = "Update";
    var id = $routeParams.editId;
    $scope.lastChecked = null;
    $scope.item = Item.get({ id: id });
    $scope.checkMe = function(id){
        if($scope.lastChecked=== id){
            $scope.lastChecked= null;
        }else{
            $scope.lastChecked= id;
        }
    };
    $scope.amIChecked = function(id){
        return $scope.lastChecked=== id;
    };
    $scope.save = function () {
        Item.update({ id: id }, $scope.item, function () {
            $location.path('/');
        });
    };
};