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;
Community
  • 1
  • 1
Ivan Chernykh
  • 41,617
  • 13
  • 134
  • 146
  • sure seems like `delete $parse('very.very.deep.property')` should work – charlietfl Dec 01 '13 at 08:11
  • @charlietfl no , i thought that `delete $parse('very.very.deep.property').assign($scope)` will work, but it only sets the `property` to `undefined` : http://jsfiddle.net/cherniv/2Evxq/ – Ivan Chernykh Dec 01 '13 at 09: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 – charlietfl Dec 01 '13 at 09: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 – Ivan Chernykh Dec 01 '13 at 09: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? – charlietfl Dec 01 '13 at 09:47
  • @charlietfl yes , i guess many workarounds can be found , but i'm very interested to find the real solution for the issue ;) – Ivan Chernykh Dec 01 '13 at 09:54
  • @charlietfl data chunk that contains this string comes in one server call with its small view , like : `
    `
    – Ivan Chernykh Dec 01 '13 at 10:03

1 Answers1

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.

Nicolas ABRIC
  • 4,925
  • 28
  • 18