AngularJS - 深层对象'属性删除

Che*_*niv 6 javascript object angularjs angularjs-service angularjs-scope

我知道如何使用$parse服务设置对象'"deep"属性,就像在这篇文章中一样.

但是我如何删除深层财产?不要将其分配给null:

$parse('very.very.deep.property').assign($scope, null);
Run Code Online (Sandbox Code Playgroud)

,但实际上删除它,就像我们在JavaScript中这样做:

delete $scope.very.very.deep.property;
Run Code Online (Sandbox Code Playgroud)

Nic*_*RIC 5

我担心没有Angular服务/功能可以满足您的需求.但您仍然可以实现以下内容以满足您的要求:

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)
}
Run Code Online (Sandbox Code Playgroud)

这里有一个小提琴.希望这会派上用场.