AngularJS将字符串转换为引用$scope中的对象的对象/数组
AngularJS Convert String to Object/Array that references the objects in the $scope
我在 angular 中创建了一个指令,其范围不是孤立
的<div ng-controller="myController">
<ul>
<li myDirective="model[1].list" >a lot more things will happen inside here</li>
</ul>
</div>
控制器:
app.controller("myController",["$scope",function($scope){
$scope.model = [
{
list:[ "object1","object2","object3" ]
},
{
list:[ "object4","object5","object6" ]
},
{
list:[ "object7","object8","object9" ]
}
]
}]);
命令:
app.directive("myDirective",[function(){
return {
scope:true,
controller:function($scope,$element,$attrs){
/*
How do I directly be able to manipulate from inside here what is assigned
on the $attrs.myDirective
without ISOLATING the scope of the directive
*/
}
}
}]);
我使用的解决方案之一是这个函数,我把它放在指令中并处理分配给 $attrs.myDirective 的字符串,它与"model.list"配合得很好但是对于"model[2].list",它不会,因为它不是为它而构建的。如何修改此功能或是否有更好的解决方案来解决此问题...
$scope.rediks = function(string)
{
var scope = $scope;
var scopeSplit = string.split('.');
for (i = 0; i < scopeSplit.length - 1; i++)
{
scope = scope[scopeSplit[i]];
if (scope == undefined) return;
}
return scope[scopeSplit[scopeSplit.length - 1]];
}
谢谢
使用 $parse
服务 (docs) 获取属性指向的值:
controller: function($scope,$element,$attrs,$parse) {
var value = $parse($attrs.myDirective)($scope);
// for the above case, value = [ "object4","object5","object6" ]
}
相关文章:
- Javascript(Angular)从一个对象数组到第二个数组查找值
- 在Javascript中转换对象数组
- 在JavaScript中通过索引从对象数组中获取值
- Backbone虹吸以获取对象数组
- 如何在DataTables 2.1中迭代对象数组
- Javascript-根据赋值顺序,按键合并对象数组
- 将事件附加到对象/数组
- jQuery$.inArray()总是返回-1和一个对象数组
- javascript处理一个对象数组以获得一个新的对象数组
- javascript在数组中获取对象数组中键的所有不同值
- 在对象数组中查找多个值的d3范围
- Undercore.js获取对象数组中键对象的值
- 在mongoose中使用正则表达式在对象数组中进行查询搜索
- 如何通过json对象数组为嵌套对象赋值
- 如何循环通过2个对象数组并通过匹配id进行合并
- 为对象数组创建列表项
- 如何使用javascript合并两个对象数组
- JSON到对象数组,并向每个对象添加项
- JavaScript:从对象数组中获取唯一值及其计数
- 按不同项目对对象数组进行排序