2017-06-01 27 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+ .. –