During the upgrade process of Font Awesome 5 from 4.7.0, I noticed that any class bindings I gave to an <i>
tag would not function as it did before.
Imagine the following element with a class binding:
<i class.bind="iconClass"></i>
And imagine the initial value of the iconClass
being 'fas fa-cog'
. When changing the value of iconClass
to 'fas fa-ship'
, the icon won't update to the newly set icon classes. It will remain a cog icon.
I believe this happens because Font Awesome 5 replaces the <i>
tags with <svg>
tags and doesn't copy over the class binding properly and thus not trigger an icon change.
In the following example, the bound classes are changed after two seconds to illustrate the problem, please see this GistRun for an example of the issue. See app.html
and app.js
for the implementation. It also contains a dirty workaround.
How can/should this behaviour be implemented?
Inspired by the answer of @huocp utilising innerhtml
.
Using a custom component to solve this problem does indeed work. However, as I will only be having one icon with a class binding in my project, I have found a simpler way of doing this:
<i innerhtml="<i class='${iconClass}'></i>"></i>
A change of the iconClass
value will generate a new <i>
child (while replacing the old <svg>
element) within the parent, after which Font Awesome 5 will convert this <i>
child into an <svg>
.
Any elements can be used as parent and child elements of course, I just think <i>
looks short and clean, it will nest the <svg>
generated by Font Awesome 5 within the <i>
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With