通过Angular从UL中删除LI

时间:2014-11-25 20:36:43

标签: javascript jquery html angularjs

我的MVC控制器填充了无序列表。在那个无序列表中,我在每个列表项上都有一个按钮,允许用户从列表中删除该列表项。

代码在DB意义上有效,但在UI中,该行仍然存在于屏幕上。

工作原理: HTML:

<div ng-hide="history.distLists.length == 0">
    <strong>Member of Distribution Lists:</strong>
    <div style="overflow-y: scroll; width: 100%; height: 120px;">
        <div ng-repeat="list in history.distLists">
            <span class="span9 offset1" style="min-height: 25px;">{{list.ListName}}</span>
            <span class="span1" style="min-height: 25px;">
                <button ng-show="history.currentUserIsRep" class="btn-link" ng-click="dlRemove({{list.ListId}})"><span class="btn-danger">&nbsp;X&nbsp;</span></button>
            </span>
        </div>
    </div>
</div>

controller.js:

    $scope.dlRemove = function (listId) {
        api.dlRemove($routeParams.id, listId, function (result) {
            if (result.error) {
                notificationService.error('<h4>An error occurred removing the customer from the Distribution List.</h4>' + result.error);
            } else {
                $scope.history.distLists.splice(listId, 1);
                notificationService.success('Customer removed from Distribution List.');
            }
        });
    };

splice是抛出错误的地方: TypeError:无法读取属性&#39; splice&#39;未定义的   在localhost / scripts / myPage / controllers.js:147:37

147是拼接线,37是&#34; distLists&#34;。

我的目标只是让dang行在成功从数据库中删除后消失。

1 个答案:

答案 0 :(得分:2)

除了你创建UL元素而不是LI之外,我注意到你将list.ListId传递给了dlRemove函数

list.ListId listdistLists项的索引或关键字是$index吗?除非是这样,否则您还需要将splice传递给该函数,以便可以使用$index调用{{1}}。

我发现开发人员工具和/或扩展程序(如Batarang和ng-inspector)在调试范围方面非常出色

相关问题