6

I know how to set object' "deep" property with $parse service , like in this post.

But how can i delete a deep property ? Not assign it to null with this:

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

, but actually delete it, like we're doing it in JavaScript:

delete $scope.very.very.deep.property;
7
  • sure seems like delete $parse('very.very.deep.property') should work Commented Dec 1, 2013 at 8:11
  • @charlietfl no , i thought that delete $parse('very.very.deep.property').assign($scope) will work, but it only sets the property to undefined : jsfiddle.net/cherniv/2Evxq Commented Dec 1, 2013 at 9:04
  • so am curious why you can't use delete $scope.very.very.deep.property; because that does leave deep as empty object and hasOwnProperty('property') is false Commented Dec 1, 2013 at 9:37
  • @charlietfl because this part : very.very.deep.property come to me as a string from server.. in the end it is translated to XML node and is a part of one big XML structure Commented Dec 1, 2013 at 9:40
  • ok...so do you have to recursively loop through deep nodes of the xml? If so while looping through mabe could keep adding [nodeName] then [nodeName][childNodeName] to scope at each level? Commented Dec 1, 2013 at 9:47

1 Answer 1

5

I am afraid that there is no Angular service/function for what you are looking for. But you can still 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's 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)
}

A fiddle is available here. Hope this would come handy.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.