Remove current node from list: AngularJs

Fiddle  link, where we can add new child nodes, and their child element node can also remove the child element node only for the parent.

I am trying to delete all child nodes and ALSO the current node from the same delete button. (All child nodes are deleted successfully, but I cannot remove the current node from the list.)

How can I do this with Angular Js.

    angular.module("myApp", []).
controller("TreeController", ['$scope', function($scope) {
    $scope.delete = function(data) {
        data.nodes = [];
    };
    $scope.add = function(data) {
        var post = data.nodes.length + 1;
        var newName = data.name + '-' + post;
        data.nodes.push({name: newName,nodes: []});
    };
    $scope.tree = [{name: "Node", nodes: []}];
}]);

Someone can help.

+4
source share
1 answer

To remove the current node, you need to remove it from the parent, so you will need to save the link to the parent node in the data

data.nodes.push({name: newName,nodes: [], parent: data});

Then in delete () you can access the parent node and delete yourself:

$scope.delete = function(data) {
    var index = data.parent.nodes.indexOf(data);
    if(index > -1) {
        data.parent.nodes.splice(index, 1);
    }
};

jsfiddle

+2

All Articles