【问题标题】:AngularJS adding a new item and removing an existing item from a listAngularJS 添加新项目并从列表中删除现有项目
【发布时间】:2016-01-26 18:31:15
【问题描述】:

(1) 我正在尝试将新用户添加到项目列表 (userList)。该功能有效,但存在一个问题。我将列表设置为“可选”,又名.. 当用户单击列表中的某个项目时,我的 html5 代码中的文本框将填充列表中所选项目的值。这允许用户编辑项目的各个属性。文本框组的正下方是我的“添加新用户”按钮.....当应用程序第一次运行时,文本框是空的,我用适当的文本填充它们并单击添加按钮,新用户被附加到列表。但是问题是,当我已经选择了一个项目时,对其进行了编辑......然后文本框仍然填充有项目值......现在如果我点击添加新用户......添加了一个新用户......但现在我的列表中有重复的用户.. 这很好,因为我总是可以编辑其中一个......但是.. 看起来新用户和旧用户现在都以某种方式链接......如果我编辑其中一个,另一个的值也会改变......(我希望这是有道理的)。我觉得因为新用户是通过旧用户的选定记录创建的,所以它们的索引以某种方式相关....似乎无法弄清楚如何在没有旧用户连接的情况下创建新用户。

(2) 删除用户可以正常工作,但除此之外,删除的用户始终位于列表底部。我希望能够选择列表中的任何项目并删除该特定项目。我尝试使用类似的东西:-

$scope.userList.splice($scope.userList.indexOf(currentUser), 1);

但无济于事。

我的 Javascript:-

<script type="text/javascript">
    function UserController($scope) {
        $scope.userList = [
           { Name: "John Doe1", Title: "xxxx", Company: "yyyy", Place: "zzzz" },
           { Name: "John Doe2", Title: "xxxx", Company: "yyyy", Place: "zzzz" },
           { Name: "John Doe3", Title: "xxxx", Company: "yyyy", Place: "zzzz" },
           { Name: "John Doe4", Title: "xxxx", Company: "yyyy", Place: "zzzz" }
        ];


        $scope.selectUser = function (user) {
            $scope.currentUser = user;
        }

        $scope.addNew = function (currentUser) {
            $scope.userList.push(currentUser);
            $scope.currentUser = {}; //clear out Employee object
        }

        $scope.removeItem = function (currentUser) {
           // $scope.userList.pop(currentUser);
            $scope.userList.splice($scope.userList.indexOf(currentUser), 1);
            $scope.currentUser = {}; //clear out Employee object
        }
    }
</script>

我的 HTML:-

<div class="row">
        <div style="margin-top: 40px"></div>
        <div data-ng-app="" data-ng-controller="UserController">
            <b>Employee List</b><br />
            <br />
            <ul>
                <li data-ng-repeat="user in userList">

                    <a data-ng-click="selectUser(user)">{{user.Name}} | {{user.Title}} | {{user.Company}} | {{user.Place}}. </a>

                </li>
            </ul>
            <hr>
            <div style="margin-top: 40px"></div>
            <b>Selected Employee</b><br />
            <br />
            <div style="border:dotted 1px grey; padding:20px 0 20px 0; width:40%;">
            <div class="row" style="margin-left: 30px">
                <div style="display: inline-block;">
                    Name: 
                </div>
                <div style="display: inline-block; margin-left: 35px;">
                    <input type="text" data-ng-model="currentUser.Name">
                </div>
            </div>
            <div style="margin-top: 20px"></div>
            <div class="row" style="margin-left: 30px">
                <div style="display: inline-block;">
                    Title: 
                </div>
                <div style="display: inline-block; margin-left: 45px;">
                    <input type="text" data-ng-model="currentUser.Title">
                </div>
            </div>
            <div style="margin-top: 20px"></div>
            <div class="row" style="margin-left: 30px">
                <div style="display: inline-block;">
                    Company: 
                </div>
                <div style="display: inline-block; margin-left: 10px;">
                    <input type="text" data-ng-model="currentUser.Company">
                </div>
            </div>
            <div style="margin-top: 20px"></div>
            <div class="row" style="margin-left: 30px">
                <div style="display: inline-block;">
                    Place:
                </div>
                <div style="display: inline-block; margin-left: 35px;">
                    <input type="text" data-ng-model="currentUser.Place">
                </div>
            </div>
            </div>
            <div>
                <div style="margin: 2% 0 0 8%; display:inline-block">
                    <button data-ng-click="addNew(currentUser)" class="btn btn-primary" type="button">Add New Employee</button>
                </div>
                <div style="margin: 2% 0 0 1%; display:inline-block">
                    <button data-ng-click="removeItem(currentUser)" class="btn btn-primary" type="button">Delete Employee</button>
                </div>
            </div>
            <hr>
            <div style="margin-top: 40px"></div>
            <b>Employee Details:</b><br />
            <br />
            {{currentUser.Name}} is a {{currentUser.Title}} at {{currentUser.Company}}. He currently lives in {{currentUser.Place}}. 
        </div>
    </div>

* EDIT * 我解决了删除用户问题:-

 $scope.removeItem = function (currentUser) {
                if ($scope.userList.indexOf(currentUser) >= 0) {
                    $scope.userList.splice($scope.userList.indexOf(currentUser), 1);
                    $scope.currentUser = {}; //clear out Employee object
                }
            }

感谢建议,添加新用户的问题也得到了解决。

【问题讨论】:

    标签: javascript .net angularjs


    【解决方案1】:

    你有两个问题。在$scope.addNew():

    $scope.userList.push(currentUser);
    

    此行推送对您当前正在编辑的 same 对象的引用。这就是用户出现链接的原因,因为您在列表中有两次相同的对象。相反,您需要将对象的属性复制到一个新对象上,在这种情况下您可以使用angular.extend():

    $scope.userList.push(angular.extend({}, currentUser));
    

    您可能会考虑让“添加新”按钮只是将一个空白用户添加到列表中并选择它进行编辑:

    $scope.addNew = function () {
        $scope.userList.push($scope.currentUser = {});
    };
    

    在$scope.removeItem() 中,您使用数组的pop() 方法尝试删除特定项,但pop() 删除了最后一个 项并且实际上不接受任何参数:

    $scope.userList.pop(currentUser);
    

    您可以迭代列表以删除特定对象:

    var i;
    for (i = 0; i < $scope.userList.length; ++i) {
        if ($scope.userList[i] === currentUser) {
            $scope.userList.splice(i, 1);
            break;
        }
    }
    

    或者您可以使用indexOf(),但测试返回值不等于-1,否则splice() 调用将从列表中删除最后一个元素。

    【讨论】:

    • 谢谢!!我在您发布答案时解决了删除问题:) 我使用以下删除方法:- $scope.removeItem = function (currentUser) { $scope.userList.splice($scope.userList.indexOf(currentUser), 1); $scope.currentUser = {}; //清除Employee对象}
    • @Philo 这也是一个好方法,但我会在调用splice() 之前验证indexOf() 不会返回-1。否则,如果您尝试删除不在列表中的项目,则会从列表中删除最后一项。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多