1

I need to set these properties when click on the div in angular with javascript:

maximunWidth() {
    var parentDiv = document.querySelector(".cadr");
    parentDiv.setAttribute("width",(this.size.width - 2).toString());
    parentDiv.setAttribute("height",(this.size.height - 41).toString());
}

and this is my html code :

<div id="cadr" class="cadr col-md-6 col-xl-6 col-sm-12 col-lg-6"></div>
<div class="minmize" (click)="maximunWidth()"></div>

What's the problem? how can I solve this problem?

3
  • What version of angular are you using? Did you try ng-click="maximunWidth()" in your HTML (if you're using angularjs v1.x)? Commented Aug 22, 2019 at 7:05
  • i using the click . angular version 8 Commented Aug 22, 2019 at 7:06
  • Possible duplicate of Set component style from variable in Angular 2 Commented Aug 22, 2019 at 7:23

4 Answers 4

1

It is not recommended to change the dom element directly you can use ngStyle directive

template

<div id="cadr" class="cadr col-md-6 col-xl-6 col-sm-12 col-lg-6" 
     [ngStyle]="{'width.px':width ,'height.px':height  }"></div>
<div class="minmize" (click)="maximunWidth()"></div>

component

public width = 'initial'; // 👈 default value 
public height  = 'initial'; // 👈 default value 

public maximunWidth() { 
 this.width = this.size.width - 2;
 this.height = this.size.height - 41;
}
Sign up to request clarification or add additional context in comments.

Comments

1

Try to avoid accessing the html elements directly. The whole point of Angular is (very much simplified) to take care of the DOM for you.

Instead of setting attributes directly let Angular take care of this for you:

<div id="cadr" class="cadr col-md-6 col-xl-6 col-sm-12 col-lg-6"
     [ngStyle]="{'width': calculatedWidth, 'height': calculatedHeight}"></div>
<div class="minmize" (click)="maximunWidth()"></div>

And populate these values with the computed values in your typescript:

maximunWidth() {
    this.calculatedWidth = ...;
    this.calculatedHeight = ...;
}

Comments

1
<div id="cadr" class="cadr col-md-6 col-xl-6 col-sm-12 col-lg-6"  [style.width]="widthDiv+'%'" [style.height]="heightDiv+'%'"></div>
<div class="minmize" (click)="maximunWidth()"></div>

or,

<div id="cadr" class="cadr col-md-6 col-xl-6 col-sm-12 col-lg-6"  [style.width]="widthDiv+'px'" [style.height]="heightDiv+'px'"></div>
<div class="minmize" (click)="maximunWidth()"></div>

.ts

public maximunWidth() { 
 this.widthDiv= this.size.width - 2;
 this.heightDiv= this.size.height - 41;
}

Comments

1

In your way of implementation whenever you click it will decrease the dimesion of that div tag. Try using property binding.

cssClass = 'oldSize'
maximunWidth() {
    this.cssClass = 'newSize'
}

In your html

<div id="cadr" [class]=" 'cadr ' + 'col-md-6 ' + 'col-xl-6 ' + 'col-sm-12 ' + 'col-lg-6 ' + cssClass"></div>
<div class="minmize" (click)="maximunWidth()"></div>

2 Comments

there is a small error 'cadr' 'col-md-6' => 'cadr' + 'col-md-6' ... 👍
I think it 's better to use [ngClass ] ="{'cadr col-md-6 col-xl-6 col-sm-12 col-lg-6' : true ,newSize : toggleState , oldSize : !toggleState" I just like to use ngStle and ngClass 😅😅

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.