0

I wanted to render three cards with given contents in array using string interpollation. As well as set the style property of the specific card according to the given string in the array.

But I am having issue setting style property to my card. Issue is whenever I try to set style using string interpolation my view just displays blank.

 <div class="row-sm-2" *ngFor="let post of classifications">
     <mat-card style={{ post.margin }}>
         <p style="color: #62697D;">{{ post.content }}</p>
     </mat-card>
 </div>
import { Component } from '@angular/core';


@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {

  classifications = [
    {margin: '0px; width: 100%', content: 'Hello there'},
    {margin: '20px; width: 100%', content: 'Hi there'},
    {margin: '20px; width: 100%', content: 'Well its angular'}
  ];

}
1
  • missing quotes? <mat-card style="{{ post.margin }}"> Also check the console. Commented Jun 18, 2020 at 6:17

3 Answers 3

2

First, you are missing quotes, <mat-card style="{{ post.margin }}">

Secondly, this will resolve to a rubish markup like

<mat-card style="0px; width: 100%"> which is invalid - 0px of what?

use ngStyle to set element style dynamically.

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

1 Comment

Yeh I did saw that mistake and corrected it. But ngStyle is not working in my case. Instead I just only used [style]
1

Either enclose it in quotations, or bind to [style]. Also it'd be better to structure the data the following way and access the styles using post.style instead of post.margin.

classifications = [
  {style: 'margin: 0px, width: 100%', content: 'Hello there'},
  {style: 'margin: 20px, width: 100%', content: 'Hi there'},
  {style: 'margin: 20px, width: 100%', content: 'Well its angular'}
];
<div class="row-sm-2" *ngFor="let post of classifications">
  <mat-card style="{{ post.style }}">
    <p style="color: #62697D;">{{ post.content }}</p>
  </mat-card>
</div>

Or

<div class="row-sm-2" *ngFor="let post of classifications">
  <mat-card [style]="post.style">
    <p style="color: #62697D;">{{ post.content }}</p>
  </mat-card>
</div>

2 Comments

Well first solution doesn't works. And your second solution is pretty much close to the answer. Instead of [ngStyle] I only used [style] and it worked.
Yeah I've edited the answer. [ngStyle] expects an object as input. But you might have to modify the actual string that denotes the style. '0px; width: 100%' isn't a valid style.
0
<div class="row-sm-2" *ngFor="let post of classifications">
    <mat-card [style]= 'post.margin'>
        <p style="color: #62697D;">{{ post.content }}</p>
    </mat-card>
</div>

The issue was I was doing string interpolation instead I wanted to do is property binding all I have to do was remove curly braces

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.