I have a simple directive which repeats a section of transcluded content twice. Like this.
link: function (scope, element, attrs, controller, transclude) {
transclude(scope.$parent, function(clone) {
element.find('[transclude-main]').replaceWith(clone);
});
transclude(scope.$parent, function(clone) {
element.find('[transclude-overflow]').replaceWith(clone);
});
});
This works mainly as intended but if the content contains a form then I end up with two forms with the same name.
More importantly my main page controller (customers) only has reference to one of the forms (customers.myForm) so if I try to reset the form or call any other form controller functions only one of the forms changes, obviously.
So, I tried to modify my code to look for forms and change the form name to something new, like this.
link: function (scope, element, attrs, controller, transclude) {
transclude(scope.$parent, function(clone) {
element.find('[transclude-main]').replaceWith(clone);
});
transclude(scope.$parent, function(clone) {
clone.find('FORM').each(function() {
$(this).attr('name', $(this).attr('name') + '2');
});
element.find('[transclude-overflow]').replaceWith(clone);
});
});
This does actually modify the HTML for me and I end up with two forms - myForm and myForm2.
The problem is that there is still only one reference to myForm in my main controller. The first one works but the second one doesn't. I can only assume that they are somehow compiled against the scope.$parent which I'm passing into the transclude function before I'm messing about with the clone? If that's the case I'm not sure how to fix it.
EDIT:
Added a plunkr here:
https://plnkr.co/edit/XE7REjJRShw43cpfJCh2
If you open a dev console you'll see that I'm using console.log to write out the contents of myForm and myForm2 which should be the two copies of the form in my second toolbar. myForm2 doesn't exist and I suspect this is because it's compiled against the parent scope before it's cloned.
FORM
element that doesn't exist? – Garrot