bug: Issues with the duplication of HTML templates
Type: bug
Platform: all
Copied from the forum: http://forum.ionicframework.com/t/about-ionic-duplicating-some-html-elements/16917
Hi,
Ionic dupicates some HTML elements in order to (I believe) achieve smoother transitions. For instance (there may be others), in an ionNavBar it duplicates its content in 2 blocks such as these, and one only is visible except during transitions:
<div nav-bar="active" class="nav-bar-block"> ... </div>
<div nav-bar="cached" class="nav-bar-block"> ... </div>
This seems quite smart, but I'm concerned with the drawbacks it implies:
- An element with an HTML ID will be duplicated. I believe having 2 elements with the same ID in the DOM is forbidden in the HTML spec. Using document.getElementById will only return one of these elements.
- I created a directive that listens for an event on the document: document.addListener('menubutton', ...) . I used that directive in the navbar. As Ionic duplicates the content of the navbar, my event listener is attached twice, and always fires twice.
- Performance-wise, Angular has twice as many watchers to maintain for the code in navbars. That doesn't sound so great.
- There are probably some other issues I haven't thought of yet or encountered.
Have these issues been considered ? Is there a way to deal with this, or should we consider not duplicating the templates any more for future Ionic versions ?
Cheers Greg
As far as the navbar goes there will only be two elements, and they work together for smooth transitions. These elements are created and managed by ionic, and they don't use IDs.
That's not true. These elements are created via Angular templates, and those are in control of the user. The navbar element itself may be safe, but its contents are duplicated as well.
Sample from your navBar documentation:
<ion-view view-title="myTitle">
<ion-nav-bar class="bar-positive">
<ion-nav-back-button>
</ion-nav-back-button>
<div class="buttons primary-buttons">
<button class="button">
Button
</button>
</div>
</ion-nav-bar>
</ion-view>
Nothing prevents the user from putting an ID on the button. However, getElementById will not work deterministically, just for the sake of a fancy animation. CSS styling based on ID may fail too, depending on the rendering engine.
The performance impact, and the issues with attaching DOM events to elements inside the navbar, remain as well. I don't see how you can consider the issue is closed. The least you should do is warn your users that the navbar component breaks those HTML basics.
@adamdbradley I'm also encountering this issue. In my index.html I have a button on the navbar that shows my "composer" that is accessible from any tab:
<ion-nav-bar class="bar-stable nav-title-slide-ios7" ng-if="!vars.singlePostMode">
<ion-nav-buttons side="right">
<button class="button icon button-clear ion-ios7-compose-outline navbutton" ng-click="showComposer()" ng-controller="ComposerCtrl" id="composerButton">
</button>
</ion-nav-buttons>
</ion-nav-bar>
2 problems:
- I add an id to it so that in my guided tour when the app starts, I can attach a popover to it saying "Tap here to create a new post". This doesn't work because, as noted above, there are now two elements with the same id.
- The controller ComposerCtrl gets created twice. That causes problems, for instance when trying to broadcast an event to that controller, it gets received and acted on twice.
I spent hours trying to figure out why this was all happening, so I agree with @gouegd that at the very least some documentation about this would be nice. I've had to implement some hacky workarounds, so it seems like it might be worth revisiting how things work.
I moved away from Ionic. With this bug ignored, I still feel sad for the people using it. Three monkeys
@adamdbradley , why [nav-bar="cached"] has 'display:block' ? this is defined in ionic.css at line 7640 in v1.1.0
Yeah I wasted a hour on this until i realized it was duplicating elements with ids.
Unfortunately i have just spent a few hours banging my head over how an id field was being duplicated and thus failing my protractor tests.
It would be nice to have this documented as a testing gotcha for future reference.
what a crap
Is the violation of basic html rules really a good trade off for smooth transitions?
+1
This is giving me problems handling elements that I want to place in the header bar, is at least possible to disable that duplication of the navbar?
I just ran into this myself. Was a PITA to find any information on this until I stumbled across this issue. Is there really no other way around this? The duplication of elements inside the nav bar (and thus, IDs) is causing issues with our automated testing.
Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of Ionic, please create a new issue and ensure the template is fully filled out.