问题描述
我想使用 angular' 机制进行深度属性嵌套,这是 ng-model
指令正在使用的.我的意思是我们可以通过在视图中编写: ng-model="data.obj1.prop2.attr3.value4.text"
在一个范围内创建非常深"的对象,所以我想这样做在控制器/服务中也很容易.我不想重新发明轮子(或使用 this 或 这个).是否有诸如 angular.create(path_str)
之类的未记录内容?
I want to use angular' mechanism for deep property nesting , something that ng-model
directive is using. I mean we can create very "deep" object in a scope by writing : ng-model="data.obj1.prop2.attr3.value4.text"
in a view , so i want to do it easily in a controller/service too. I don't want to reinvent a wheel (or use this or this). Is there something undocumented like angular.create(path_str)
?
推荐答案
您可以实现的一种方法是使用 $parse
服务.它有 getter
和 setter
功能,我认为可以处理你想要的
One way you can achieve is by using $parse
service. It has getter
and setter
function that i think can handle what you want
var getter = $parse('prop1.prop2.prop3.prop4');
var setter = getter.assign;
setter($scope,"value1111");
看这个小提琴http://jsfiddle.net/cmyworld/m7gxn/
我认为这也有效
$scope.$eval("prop2.prop2.prop3.prop4=55");
这篇关于ngModel 的深层对象创建机制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!