AngularJS: ng-repeat list is not updated when a model element is spliced from the model array

后端 未结 5 2056
走了就别回头了
走了就别回头了 2020-11-27 12:42

I have two controllers and share data between them with an app.factory function.

The first controller adds a widget in the model array (pluginsDisplayed) when a link

5条回答
  •  粉色の甜心
    2020-11-27 13:30

    There's an easy way to do that. Very easy. Since I noticed that

    $scope.yourModel = [];
    

    removes all $scope.yourModel array list you can do like this

    function deleteAnObjectByKey(objects, key) {
        var clonedObjects = Object.assign({}, objects);
    
         for (var x in clonedObjects)
            if (clonedObjects.hasOwnProperty(x))
                 if (clonedObjects[x].id == key)
                     delete clonedObjects[x];
    
        $scope.yourModel = clonedObjects;
    }
    

    The $scope.yourModel will be updated with the clonedObjects.

    Hope that helps.

提交回复
热议问题