AngularJS - Deleting a Deep Object

I know how to set a deep property object using $parse , as in this post .

But how can I remove a deep property? Do not set it to null as follows:

 $parse('very.very.deep.property').assign($scope, null); 

but actually remove it, as we do it in JavaScript:

 delete $scope.very.very.deep.property; 
+6
source share
1 answer

I am afraid there is no Angular service / function for what you are looking for. But you can implement something like the following to fulfill your requirement:

 function Ctrl($scope,$parse){ var path = 'very.very.deep.property'; var partials = path.split('.'); var deepKey = partials.pop(); //Extract the key name from your path var deepPath = partials.join('.'); //Build the parent path var deep = $parse(deepPath); var prop = $parse(path); prop.assign($scope, "I'm so deep"); delete deep($scope)[deepKey]; //Evaluate the deep path against your scope and delete the key console.log(deepKey, $scope.very.very.deep) } 

The violin is available here . Hope this comes in handy.

+5
source

Source: https://habr.com/ru/post/959109/


All Articles