0

I am having issues dynamically updating an elements style.

I am overlaying some margins on top of an image. I created a child component to handle this. I call a function on my child component which calculates the margins and then sets a variable.

Here is the parent component call, which is triggered by a button click.

showMargins() {
    if (this.showingMargins) {

        this.showingMargins = false;
    } else {
        this.showingMargins = true;
        this.marginComponent.calculateMargins();
    }
}

And here is my child component:

marginStyles: any = {};
...
calculateMargins() {
    //logic 
    ...
    //set the property
    this.marginStyles = {
       'height.px': height,
       'margin-left.px': left
    }
}

And then in my template

<div id="margin-left" [ngStyle]="marginStyles">

I've tried using a function as well

private leftDiv() {
    debugger;
    return this.marginStyles;
}

thus changing my template to

<div id="margin-left" [ngStyle]="leftDiv()">

but to no avail. If I hardcode the values when I declare the variable it works fine.

    marginStyles: any = {'height.px': 200, 'margin-left.px': 20};

And if I hardcode it in the template it works fine as well; it's just failing after I've done my calculations and won't render the lines that I'm overlaying. Where have I gone wrong? If it helps, I'm on Angular 4.

Edit: I moved all of my logic to my parent component, and everything looks fine. It appears to me that the values I am for marginStyle are not being updated in the child component, which is baffling to me. They should be updating. ngStyle is working fine, it's the variable that is not being updated.

9
  • 1
    Where the function calculateMargins() is called? Commented Jan 9, 2019 at 17:11
  • that is being called by the parent component. Commented Jan 9, 2019 at 17:11
  • 1
    Could you provide small demo on stackblitz.com/fork/angular ? Commented Jan 9, 2019 at 17:13
  • return this.leftMarginStyles; or return this.marginStyles; ? Commented Jan 9, 2019 at 17:17
  • 1
    Try showing the style in the view with <div>{{ marginStyles | json }}</div>. It may not be in a valid state (height and left may not be valid numbers in your code). Commented Jan 9, 2019 at 17:18

2 Answers 2

2

I've implemented the similar code

https://stackblitz.com/edit/angular-cvayak

and it seems to be working as expected. Please, check for the differences. Pay attention to the Element referencing by means of ViewChild decorator, all functions should be public, ElementRef type is the referencing component class because of typization.

Sign up to request clarification or add additional context in comments.

2 Comments

Beat me to it, that's essentially what I am doing.
I'm not seeing a difference in my source code vs what we've implemented in stackblitz, which is confusing to me why it'd work there but not in my project
0

I think you should return marginStyles instead of leftMarginStyles

Comments

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.