Angular 12 doesn't render svg component
NickName:Barış Can Yılmaz Ask DateTime:2021-06-29T23:19:29

Angular 12 doesn't render svg component

I am developing an angular component. it is an avatar creater using with svg. My problem is that when I run my component in angular project it draws just a circle not other components like nose,hair,face etc. ,but when I inspect my component and copy its svg elements and paste in a svg viewer, it shows as how should it be.

My Component in local enter image description here

As you can see, my component has svg elements but doesn't rendered in browser. But,when I use same svg elements in a viewer they are rendered.

My Component's svg elements in online svg viewer enter image description here

Also, here is project's repo

Angular project version

enter image description here

Any help is appreciated

Copyright Notice:Content Author:「Barış Can Yılmaz」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/68181533/angular-12-doesnt-render-svg-component

Answers
Barış Can Yılmaz 2021-07-01T20:23:18

Problem is that, when I create components with selector tags , angular adds its own element tags to svg schema and this breasks the svg's schema and browser doesn't render it. So, I used attribute selector for achieving this problem. This SO answer help me to solve this problem.\nExample project is here. You can check components under projects folder\nI used attribute tag which is wrapped with [and ] instead of selector tags.\n@Component({\n selector: '[ngx-beard]',\n template: `\n <svg:g>\n <ng-container *ngIf="beard == 'Hipster'">\n <path\n d="M270.126 194.455C279.615 234.059 255.201 273.857 215.597 283.345C192.29 288.93 168.916 282.772 151.692 268.687C134.5 252.801 130.5 201.301 126.692 176.95C140.692 208.451 176.316 210.895 210.192 204.951C238.692 199.951 261.491 198.951 261.491 159.951C263.376 162.567 268.469 187.538 270.126 194.455Z"\n [attr.fill]="beardColor"\n />\n </ng-container>\n </g>\n `\n })\n",


dochuki 2021-06-29T15:34:58

maybe it is due to the changedetection of angular.\nHave you perhaps tried https://angular.io/api/core/ChangeDetectorRef\ndetectChanges after you change some other component?\n ngOnInit(): void {\n this.random();\n this.cd.detectChanges()\n }\n",


More about “Angular 12 doesn't render svg component” related questions

Bug on Angular 12 project

I've created an Angular12 project, but it doesn't work to get rendered at the browser (npm start), and it comes up this message: C:\Users\User\Desktop\angular\angular-coder&gt;npm start npm ERR! M...

Show Detail

Angular 12 Datepicker

I am trying to use material datepicker in Angular 12, but having error: main.js:1 ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'localeChanges' of null TypeError: Cannot read

Show Detail

Syncfusion Diagram in Angular 12 with data binding

According to the docs from Syncfusion the Diagram control (ejs-diagram) should support data binding using the datasourcesettings property (https://ej2.syncfusion.com/angular/documentation/diagram/d...

Show Detail

Angular 12 library with Angular 11 application

I created a angular library using angular 12 and added the peer dependencies of angular 11 and above.When i tried to install the library on angular version 11 application, The library got installed...

Show Detail

angular 12 webworker 404

I have updated my project from Angular 11 to Angular 12 and got everything working except of my webworkers. When I do a build, the worker.ts files are not copied to the output directory and when lo...

Show Detail

@angular/CLI issue while migrating Angular from 11 to 12

In the process of angular migration from 11 to 14. I started with 11 to 12. I ran the below command to update from angular 11 to 12 ng update @angular/core@12 @angular/cli@12, Got terminal messag...

Show Detail

Import Error from angular.json in Angular 12

Im upgrading my project from Angular 10 to 12. 10 -&gt; 11 worked fine. When compiling version 12, I got this error: Error: Should not import the named export 'projects' (imported as 'angular_const...

Show Detail

Angular 12 peerDependences

I have this issue when run npm i npm ERR! While resolving: [email protected] npm ERR! Found: @angular/[email protected] npm ERR! node_modules/@angular/common npm ERR! @angular/common@&quo...

Show Detail

Angular 12 and ngx Boostrap

I'm using Angular 12 and ngx boostrap. i try to build and i have this error : This likely means that the library (ngx-bootstrap/modal) which declares BsModalService has not been processed correctly...

Show Detail

build watch is it functional for library multi modules for Angular > 12

We've an application that use our library (external not in monorepo mode) that is implemented into multi modules. Since angular 12, the build watch doesn't work, the app is right updated but the co...

Show Detail