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 theproperty
toundefined
: 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 leavedeep
as empty object andhasOwnProperty('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
1 Answer
Reset to default 5I 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条)