You can use a recursive approach similar to your clearDivSelection method:
function isDuplicated (node, title) { var result = false; if (node.nodes && node.nodes.length > 0) result = node.nodes.reduce(function (result, node) { return isDuplicated(node, title); }, false); return result && node.name === title; }
Or (at the cost of memory), you can save a list of names:
$scope.titles = {}; $scope.add = function (data) { var post = data.nodes.length + 1; var newName = data.name + '-' + post; if ($scope.titles[newName]) return;
I'm not sure what you mean by
should reset that node value to the previous value.
if you add new objects, you will not have the โprevious valueโ, but I will leave this bit to you. Anyway, this should get you started.
Alex mcmillan
source share