简体   繁体   中英

Angular2 sibling element selector

I wanted to hide all the previous elements, which is before .map-page element.

I'm loading ionic app and I need to hide previous pages if .map-page is detected on the dom.

图片

in jQuery this is so simple $('.map-page').siblings().hide() , but in angular 2.. I'm not sure which functions to use to do that.

Since you're traversing the dom, in angular, it is suggested that you use a directive. Inside your directive, you have access to $element

EX:

app.directive('hide-siblings', function() {
    return {
        link: function($scope, $element, $attrs) {
                $element.siblings().hide()
        },
    }
});

EDIT: That is only if you have jquery and also probably not the best way. It may be better to add this directive to all your elements and use:

app.directive('hide-siblings', function() {
    return {
        link: function($scope, $element, $attrs) {
            if(!$element.hasClass('map-page'))
                $element.hide()

        },
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM