Safe way to select the current ion-view element


#1

Continuing the discussion from Please help test: Angular 1.3, improved transitions, cached views (repost):

When you use document.querySelectorAll('span'); it will select all elements from all ion-view, the only way to detect the current view, is to use the nav-view="active" attribute.

var currentView = document.querySelector('ion-view[nav-view="active"]');

currentView.querySelectorAll('span'); // Spans tags from the current view only

Shouldn’t $ionicHistory.currentView() give us the current view element?

Is ion-view[nav-view="active"] the only way to get the current view?

I’m afraid I’ll use nav-view attribute and in the future you change the values, attribute name or something else. Thanks.


#2

I created an issue https://github.com/driftyco/ionic/issues/2920


#3

I’m also looking for a solution, the above doesn’t work, in tabbed view.


#4

I’m using tabs and this works for me:

$scope.$on('$ionicView.enter', function () {
            angular.forEach(document.querySelectorAll("ion-view"), function (element) {
                if (element['$attr-nav-view'] == 'entering' || element['$attr-nav-view'] == 'active') {
                    console.log('element is the active view');
                }
            });
        });