javascript - AngularJS - Deep object' property deletion - Stack Overflow

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

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;

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;
Share Improve this question edited May 23, 2017 at 11:56 CommunityBot 11 silver badge asked Dec 1, 2013 at 7:52 Ivan ChernykhIvan Chernykh 42.2k13 gold badges136 silver badges148 bronze badges 7
  • sure seems like delete $parse('very.very.deep.property') should work – charlietfl 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/cherniv/2Evxq – Ivan Chernykh 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 – charlietfl Commented Dec 1, 2013 at 9:37
  • @charlietfl because this part : very.very.deep.property e 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 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? – charlietfl Commented Dec 1, 2013 at 9:47
 |  Show 2 more ments

1 Answer 1

Reset to default 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 e handy.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745250074a4618625.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信