Я создаю динамическую строку таблицы с полями ввода. В угловом режиме мы можем получить доступ к индексу массива, используя эту переменную $index
. Теперь моя проблема в том, что у меня есть массив подуровней, и мне нужно получить индекс родительского массива. И каждая строка имеет кнопку удаления. И мне нужно получить доступ к идентификатору родительского индекса для доступа.
Здесь примерное значение объекта:
Object
items: Array [2]
0: Object
particular_name: "SAMPLE PARTICULAR TITLE"
child_label: Object
items: Array [3]
0: Object
1: Object
2: Object
$$hashKey: "object:13"
1: Object
particular_name: "SAMPLE PARTICULAR TITLE"
child_label: Object
items: Array [0]
$$hashKey: "object:41"
В моем шаблоне у меня есть следующее:
<!-- this is the parent array -->
<div ng-repeat="item in quoteHeader.items" class="row">
<!-- some codes here... -->
<!-- 2nd level array -->
<tr ng-repeat="item in quoteHeader.items[$index].child_label.items">
<!-- need to access the parent index ID here... But index ID here is the index of the 2nd level array-->
<button type="button" class="btn btn-danger" ng-click="removeParent($index)"><span class="fa fa-times"></span></button>
JS:
$scope.removeParent = function($index) {
console.log($index);
//$scope.quoteHeader.items[$index].splice($index, 1);
}
Каждый ng-repeat
создает дочернюю область с переданными данными. Вы должны получить родительский индекс в дочерних данных с помощью $parent
. подобно
<button type="button" class="btn btn-danger" ng-click="removeParent($parent.$index)"><span class="fa fa-times"></span></button>
Другой способ использования директивы ng-init
. Просто посмотрите пример
<div ng-repeat="item in quoteHeader.items" class="row" ng-init="parentIndex = $index">
<!-- some codes here... -->
<!-- 2nd level array -->
<tr ng-repeat="item in quoteHeader.items[$index].child_label.items">
<!-- need to access the parent index ID here... But index ID here is the index of the 2nd level array-->
<button type="button" class="btn btn-danger" ng-click="removeParent(parentIndex)"><span class="fa fa-times"></span></button>