I work with a nested array with structure ...
$scope.items = [{attr1: val1,
attr2: val2,
items: [{
attr1: val1,
attr2: val2,
items: [{
...
}, ...]
}, ...]
}, ...];
which goes into ng-repeatwith ng-includelike that
<div ng-repeat="item in items" ng-include="'/path/to/template.tpl.html'"></div>
and template.tpl.html-
<div>{{item.attr1}}<\div>
<div>{{item.attr2}}<\div>
<div ng-click="fnAddNewItemBelow(item, $parent)"><\div>
<div ng-repeat="item in item.items" ng-include="'/path/to/template.tpl.html'"><\div>
Now, in the controller, I usually want to do something like
- find parent element
- find sibling element
- do siblings counts
- Find out how many levels are nested in an element.
- insert or remove items at any level of the socket
But I'm not sure how to do it elegantly. For example, imagine what I wanted to implement fnAddNewItemBelow. Two options are possible:
Areas of movement
Use the nested area structure in which Angular provides
$scope.fnAddNewItemBelow = function (item, parent) {
var newItem = ...;
parent.$parent.item.items.push(newItem);
}
, (, ng-if <div ng-click..., ... parent.$parent.$parent.item.items.push(newItem)).
, item.id
$scope.items, Angular , . $scope.items, , , , newItem
$scope.fnAddNewItemBelow = function (item) {
var newItem = ...;
fnSomeFunctionToFindItemAndInsertItemAfterIt(item.id, newItem);
}
fnSomeFunctionToFindItemAndInsertItemAfterIt (itemId, newItem) {
}
, , - .
?