2017-06-01 25 views
1

如何添加動態類到BODY從控制器在離子2? 我的代碼:離子2:如何添加從控制器類

import { Component } from '@angular/core'; 

@Component({ 
    templateUrl: 'pages.html' 
}) 
export class PagesPage { 

    constructor() { 

    } 

    addClass() { 

    //This ADD CLASS in tag BODY 

    } 

} 

回答

2

在angular 2中,我們使用引用來獲取元素。

<div #reference_id></div> 

export class App implements AfterViewInit{ 
    @ViewChild('reference_id') elem:ElementRef; 
    constructor(private rd: Renderer2) {} 
    ngAfterViewInit() { 
      console.log(this.rd); 
      console.log(this.elem.nativeElement); 
      this.rd.addClass(this.elem.nativeElement, 'new_class_name'); // this adds the class 
    } 
} 

修訂ANSWER

這裏工作plunker.

添加額外的進口

import {ElementRef, Renderer2, ViewChild, AfterViewInit} from '@angular/core'; 

檢查元素即你好在plunker結果檢查添加的類即new_class

+0

我有錯誤「無法找到名爲‘角’。」 – wstudiokiwi

+0

包括您的index.html中的角度源代碼 –

+0

您是否在談論angularjs?因爲離子2+使用角度2+ .. –