gpt4 book ai didi

javascript - 递归地在ng-repeat中迭代时如何在ng-include中获取父元素

转载 作者:可可西里 更新时间:2023-11-01 01:56:51 25 4
gpt4 key购买 nike

我制作了一个递归的 ng-repeat 元素,试图操纵事物变成了一场噩梦,因为我没有引用我正在迭代的父元素。

ng-repeat 看起来像这样:

ng-repeat="(key, value) in value"

记住它是递归的,所以 value 中的每个值都会成为新值,所以我不能只使用 ng-repeat 中的“in”值。我想做这样的事情,比如检查父级是否是一个数组,但是 $parent 是一些奇怪的东西,而不是当前迭代值的父元素。

我想做的事情的一些例子是:

ng-show="isArray(parent)"
ng-click="delete(parent, $index)"

(作为我正在做的工作的一个例子,我不得不向我的每个数组元素添加一个 ___ISARRAYELEMENT___ 属性,只是为了知道它在一个数组中 >.>;;)

编辑:基本上我想知道在我遗漏的 ng-repeat 上下文中是否有任何方便的元数据。

编辑:好的,这是完整的 html:

<html ng-app>
<head>
<title>JSON CRUD</title>
<link href="//netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css" rel="stylesheet">
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="//netdna.bootstrapcdn.com/bootstrap/3.0.3/js/bootstrap.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.6/angular.min.js"></script>
<script src="angularjsoncrud.js"></script>
<script type="text/ng-template" id="node.html">
<button ng-click="minimized = !minimized" ng-show="!isLeaf(value)" ng-init="minimized=false" ng-class="{'glyphicon glyphicon-plus': minimized, 'glyphicon glyphicon-minus': !minimized}">-</button>
<input ng-model="value.___KEY___" ng-if="!isArrayElement(value)" />
<input ng-if="isLeaf(value)" ng-model="value.___VALUE___" />
<ul ng-if="!isLeaf(value)" ng-show="!minimized">
<li ng-repeat="(key,value) in value" ng-if="key != '___KEY___' && key != '___ISARRAY___'" ng-include="'node.html'"></li>
<button type="button" ng-if="isArray(value)" ng-click="addToArray(value)">Add Element</button>
</ul>
</script>
</head>
<body ng-app="Application" ng-controller="jsoncrudctrl">
<ul>
<li ng-repeat="(key,value) in json" ng-include="'node.html'"></li>
</ul>
<pre>{{stringify(json)}}</pre>
</body>
</html>

我想做的一件事是用 isArray(parent) 替换 isArrayElement(value),因为 isArrayElement 依赖于我添加到数组中的元数据,我希望不必添加这些元数据。

最佳答案

ng-repeat 为每个元素创建一个新范围。
ng-include 还会创建一个新范围。

当您将 ng-includeng-repeat 一起使用时,会为每个元素创建 2 个作用域:

  • ng-repeat 为当前元素创建的作用域。此范围包含迭代中的当前元素。
  • 子范围ng-include 创建,继承 ng-repeat 创建的范围。

当前范围的 $parent 属性允许您访问其父范围。

本质上,当您在 node.html< 中访问 valuekey 时,ng-include 创建的作用域是空的,它实际上从 ng-repeat 创建的父范围访问继承值

在你的node.html中,访问{{value}}其实和{{$parent.value}}是一样的。因此,如果您需要访问当前元素的父元素,则必须进一步访问 {{$parent.$parent.value}}

DEMO将清理一切:

    <script type="text/ng-template" id="node.html">
<div>
Current: key = {{key}}, value = {{value}}
</div>
<div>
Current (using $parent): key = {{$parent.key}}, value = {{$parent.value}}
</div>
<div>
Parent: key = {{$parent.$parent.key}}, value = {{$parent.$parent.value}}, isArray: {{isArray($parent.$parent.value)}}
</div>
<ul ng-if="isObject(value)"> //only iterate over object to avoid problems when iterating a string
<li ng-repeat="(key,value) in value" ng-include="'node.html'"></li>
</ul>
</script>

如果您需要简化访问父级的方式,您可以尝试使用ng-includeonload 来初始化父级。像这样:

在你的顶层,没有 ng-if => 只有 2 层深

<ul>
<li ng-repeat="(key,value) in json"
ng-include="'node.html'" onload="parent=$parent.$parent"></li>
</ul>

在你的子关卡中,有 ng-if => 3 层深:

    <ul ng-if="isObject(value)">
<li ng-repeat="(key,value) in value"
ng-include="'node.html'" onload="parent=$parent.$parent.$parent"></li>
</ul>

在模板中,您可以使用 parent 访问父级,使用 parent.parent 等访问祖父级。

DEMO

关于javascript - 递归地在ng-repeat中迭代时如何在ng-include中获取父元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20876358/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com