You can dynamically change the style (width and height) of a div by adding a dynamic value to the inline [style.width] and the [style.hiegh] div property.
In your case, you can associate the width and height property of the HomeComponent class with the width and height property of a div inline style like this ... As pointed out by Sasxa
<div class="home-component" [style.width]="width + 'px'" [style.height]="height + 'px'">Some stuff in this div </div>
For a working demo, look at this plunker ( http://plnkr.co/edit/cUbbo2?p=preview )
//our root app component import {Component} from 'angular2/core'; import {FORM_DIRECTIVES,FormBuilder,AbstractControl,ControlGroup,} from "angular2/common"; @Component({ selector: 'home', providers: [], template: ` <div class="home-component" [style.width]="width+'px'" [style.height]="height+'px'">Some this div</div> <br/> <form [ngFormModel]="testForm"> width:<input type="number" [ngFormControl]="txtWidth"/> <br> Height:<input type="number"[ngFormControl]="txtHeight" /> </form> `, styles:[` .home-component{ background-color: red; width: 50px; height: 50px; } `], directives: [FORM_DIRECTIVES] }) export class App { testForm:ControlGroup; public width: Number; public height: Number; public txtWidth:AbstractControl; public txtHeight:AbstractControl; constructor(private _fb:FormBuilder) { this.testForm=_fb.group({ 'txtWidth':['50'], 'txtHeight':['50'] }); this.txtWidth=this.testForm.controls['txtWidth']; this.txtHeight=this.testForm.controls['txtHeight']; this.txtWidth.valueChanges.subscribe(val=>this.width=val); this.txtHeight.valueChanges.subscribe(val=>this.height =val); } }
Jorin Mar 09 '16 at 6:11 2016-03-09 06:11
source share