【问题标题】:AngularJS remove item from scopeAngularJS 从范围中删除项目
【发布时间】:2014-07-08 08:04:41
【问题描述】:

我的模板:

<div ng-repeat="comment in article.comments">
    {{ comment.date }} by {{ comment.author }} <button type="button" ng-click="removeComment($index)">remove</button>
</div>

我的 JSON cmets:

"comments": [
    {
        "author": "Syl A",
        "date": "2014-07-02",
        "content": "lol"
    },
    {
        "author": "Syl B",
        "date": "2014-07-02",
        "content": "lol"
    },
    {
        "author": "Syl C",
        "date": "2014-07-02",
        "content": "lol"
    }
]

我的控制器:

$scope.removeComment = function (key) {
    // This is what I want to remove
    // Object {author: "Syl A", date: "2014-07-02", content: "lol", $$hashKey: "004"}
    console.log($scope.article.comments[key]);
};

下面的sn-ps不行,什么都不做;

$scope.article.comments.splice[key, 1];

下面的sn-ps不行,对象被移除了,但是在视图中得到了“by remove”,整行没有被移除,我不能移除多个Error: [ngRepeat:dupes]

$scope.article.comments[key] = undefined;
delete $scope.article.comments[key];

下面的sn-p是不行的,对象被移除了,但是在视图中得到了“by remove”,整行没有被移除:

$scope.article.comments[key] = {};

所以我没有找到关于 SO 的解决方案来进行“一体式”删除、DOM 和“数据”。为什么我的splice 在这里不起作用?

【问题讨论】:

  • 为什么你有$scope.article.comments.splice[key, 1];而不是$scope.article.comments.splice(key, 1);
  • 我很糟糕,它现在可以工作了!

标签: angularjs angularjs-ng-repeat


【解决方案1】:

问题是你用错了拼接。

更新您的拼接以使用 () 而不是 []

$scope.article.comments.splice(key, 1);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-24
    • 2017-08-22
    • 1970-01-01
    • 2022-01-08
    • 1970-01-01
    • 2022-06-11
    • 2013-04-19
    相关资源
    最近更新 更多