javascript - Accessing parent-children objects in directive [AngularJS] -
i creating basic page shows , lets edit/add/remove children of tree. i've made recursive directive print them out, confused on how should going removing array element, need know parent call splice. appreciated, been scratching head hours :/ should passing parent element down attribute?
app.controller('treecontroller', ['$scope', function($scope) { //root object $scope.tree = { name : "root", expand : true, children : [] }; //temp data filler. $scope.filldata = function() { var child1 = { name : "element 1", expand : false, children : [] }; var child2 = { name : "element 2", expand : false, children : [] }; var subchild1 = { name : "child 1", expand : false, children : [] }; var subchild2 = { name : "child 2", expand : false, children : [] }; var grandchild1 = { name : "grandchild 1", expand : false, children : [] }; var grandchild2 = { name : "grandchild 2", expand : false, children : [] }; // add children $scope.tree.children.push(child1); $scope.tree.children.push(child2); $scope.tree.children[0].children.push(subchild1); $scope.tree.children[1].children.push(subchild1); $scope.tree.children[0].children.push(subchild2); $scope.tree.children[1].children.push(subchild2); $scope.tree.children[0].children[0].children.push(grandchild1); $scope.tree.children[0].children[1].children.push(grandchild1); $scope.tree.children[1].children[0].children.push(grandchild1); $scope.tree.children[1].children[1].children.push(grandchild1); $scope.tree.children[0].children[0].children.push(grandchild2); $scope.tree.children[0].children[1].children.push(grandchild2); $scope.tree.children[1].children[0].children.push(grandchild2); $scope.tree.children[1].children[1].children.push(grandchild2); } }]); app.directive('collection', function () { return { restrict: "e", replace: true, scope: { collection: '=' }, template: "<ul><member ng-repeat='member in collection.children' member='member'></member></ul>" } }) app.directive('member', function ($compile) { return { restrict: "e", replace: true, scope: { member: '=' }, //template: "<li></li>", templateurl: 'node.html', link: function (scope, element, attrs) { var collectionst = '<collection collection="member"></collection>'; //check if member has children if (angular.isarray(scope.member.children)) { $compile(collectionst)(scope, function(cloned, scope) { element.append(cloned); }); } scope.deleteme = function(index) { //var index = array.indexof(member); alert(array.indexof($scope.member)) }; } } });
Comments
Post a Comment