角2.x的选择DOM元素 [英] Angular 2.x selecting DOM element
问题描述
我知道这应该是容易的,但棱角2.0还没有很多例子还..
I know it should be easy but angular 2.0 has no many examples yet..
在我在某些情况下,组件之一,我需要在我身上的标签添加类。但是,我的应用程序自举于体更深,所以我需要像
In one of my components in some case I need to add class on my body tag. But my application is bootstrapped deeper than body, so I need something like
angular.element('body').addClass('fixed');
但在角2.0 ..
but in Angular 2.0..
顺便说一句,我知道我能以某种方式引导我的应用程序,包括体
标记,但我觉得在其他一些情况下,我需要选择一些元素,无论如何,所以我需要一个解决方案是如何做到这一点简单的任务 - 中选择元素,并添加类为
BTW, I know I can somehow bootstrap my application to include body
tag, but I think in some other cases I would need to select some elements anyway, so I need a solution how to do this simple task - "select element and add class to it"
推荐答案
更新
这似乎是Angular2方式
Update This seems to be the Angular2 way
import { DOM } from 'angular2/src/platform/dom/dom_adapter';
DOM.addClass(DOM.query("body"), 'fixed');
我试了一下在DART和正常工作(不知道上面的TS进口是正确的,虽然)。在DART DOM
是导出包:angular2 / angular2.dart
原始
如果您要访问DOM元素那是你的角应用程序根目录外,只要使用 document.querySelector()
,无需涉及棱角分明。
If you want to access a DOM element that's outside of your Angular application root, just use document.querySelector()
, no need to involve Angular.
这篇关于角2.x的选择DOM元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!