最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Angular ngStyle Dynamic Styling Element with ngIf - Stack Overflow

matteradmin6PV0评论

I am developing dynamically style element feature in App server authentication and database search engine.

When searchNoResult, UI is:

When click the button triggering execSearch() event -> searchSuccess is true and template searchSuccessColor takes effect (text backgroundColor turns pink). However, I can’t seem to get the color dynamically styled:

I tried setting the template as below but doesn't work:

<button class="button" 
    [disabled] = "!enableSearch"
    (click) = "execSearch()">Search State</button>
    <div *ngIf = "searchSuccess; then searchSuccessColor else searchNoResult"></div>
<ng-template #searchNoResult>
    <p>{{ stateSearchResult }}</p>
</ng-template>
<ng-template #searchSuccessColor>
    <div [ngStyle]="{backgroundColor: 'pink'}"></div>
        <p>{{ stateSearchResult }}</p>
</ng-template>

I tired ponent method as below but doesn't work:

  setColor() {
    return this.searchSuccess === true ? 'pink' : 'black';
  }

Can someone help to identify what goes wrong?

I am developing dynamically style element feature in App server authentication and database search engine.

When searchNoResult, UI is:

When click the button triggering execSearch() event -> searchSuccess is true and template searchSuccessColor takes effect (text backgroundColor turns pink). However, I can’t seem to get the color dynamically styled:

I tried setting the template as below but doesn't work:

<button class="button" 
    [disabled] = "!enableSearch"
    (click) = "execSearch()">Search State</button>
    <div *ngIf = "searchSuccess; then searchSuccessColor else searchNoResult"></div>
<ng-template #searchNoResult>
    <p>{{ stateSearchResult }}</p>
</ng-template>
<ng-template #searchSuccessColor>
    <div [ngStyle]="{backgroundColor: 'pink'}"></div>
        <p>{{ stateSearchResult }}</p>
</ng-template>

I tired ponent method as below but doesn't work:

  setColor() {
    return this.searchSuccess === true ? 'pink' : 'black';
  }

Can someone help to identify what goes wrong?

Share Improve this question edited Jun 15, 2020 at 21:10 R. Richards 25.2k10 gold badges66 silver badges65 bronze badges asked Jun 15, 2020 at 21:08 Fiona ChenFiona Chen 1,3687 silver badges16 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 3

If the else block is used only to color the background, then it could be skipped entirely using [style.background-color] and the ternary operator. Try the following

<div [style.background-color]="searchSuccess ? 'pink' : ''"><p>{{ stateSearchResult }}</p></div>

Or if you wish to use ngStyle (maybe to include more styles), it would be

<div [ngStyle]="{'background-color': searchSuccess ? 'pink' : ''}"><p>{{ stateSearchResult }}</p></div>

Or you could also use ngClass to conditionally include CSS selectors.

app.ponent.css

.highlight {
  background-color: pink;
}

app.ponent.html

<div [ngClass]="{highlight: searchSuccess}"><p>{{ stateSearchResult }}</p></div>

Update: Use a variable defined in the controller

If you wish not to hard-code the color value, you could try to define a variable in the controller that holds the color value.

Controller

export class AppComponent implements OnInit {
  backgroundColor = 'pink';

  // button `click` event handler
  execSearch() {
    ...
    this.backgroundColor = searchSuccess ? 'pink' : 'black';
  }
}

Template

<div [style.background-color]="backgroundColor">
  <p>{{ stateSearchResult }}</p>
</div>

<div [ngStyle]="{'background-color': backgroundColor}">
  <p>{{ stateSearchResult }}</p>
</div>

Notice the lack of single quotes surrounding the backgroundColor here. It denotes the variable in the controller. The single quoted values like 'pink' denote the string literals.

ngClass

CSS

.highlight-pink {
  background-color: pink;
}

.highlight-black {
  background-color: black;
  color: white;
}

Template

<div [ngClass]="searchSuccess ? 'highlight-pink' : 'highlight-black'">
  <p>{{ stateSearchResult }}</p>
</div>
Post a comment

comment list (0)

  1. No comments so far