Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular set css dynamically of :before selector

Tags:

css

angular

I need to label an upload button dynamically. My code so far:

<style>
   .file_upload_wrap.background_file:before {
    content: {{label}};  /* of course does not work */
  } 
</style>

<div class="file_upload_wrap background_file imgFormButton">
    <input type="file" class="file_upload" (change)="uploadFile($event, file)" name="file" id="file" [(ngModel)]="model.file"
      #file="ngModel">
</div>

How can I set the content dynamically?

like image 632
daniel Avatar asked Mar 26 '18 11:03

daniel


1 Answers

You can use the attr css function to retrieve the value of an attribute

https://developer.mozilla.org/en-US/docs/Web/CSS/attr

component.css

.file_upload_wrap.background_file:before {
    content: attr(data-content);  
} 

component.html

<div class="file_upload_wrap background_file imgFormButton" [attr.data-content]="label">
    <input type="file" class="file_upload" (change)="uploadFile($event, file)" name="file" id="file" [(ngModel)]="model.file"
      #file="ngModel">
</div>

Edit: stackblitz showing example https://stackblitz.com/edit/angular-muysky?file=app%2Fapp.component.ts

like image 180
David Avatar answered Sep 24 '22 08:09

David