I'm using Angular 2 rc 5
with ng2-Material alpha 7-2
.
The basic problem is that using <md-icon svgIcon='icon'>
to show the same icon from the same svg file will work in one component but not the other even though MdIconModule
and MdIconRegistry
are provided to the root AppModule
To replicate the problem
- Open this plunker
- Notice that
MdIconModule
andMdIconRegistry
are imported as part ofMaterialModule.forRoot()
into the mainAppModule
- Open the
AppComponent
and notice the call toaddSvgIconSet()
to register icons - In the template,
<md-icon svgIcon='ic_account_box_24px'>
is used to display the icon. In the view the icon is shown successfully above the nav bar. - Open
app/crisis-center/crisis.list.component
and open the Crisis Center in the view. - Note in the template, the same
<md-icon>
is present. However, no icon is displayed above the crisis list in the view. The DOM inspector from browser dev tools shows that the angular parser didn't even recognize it as an angular component (in the dom, it's left exactly as in the code)
Because I imported the icon module and service into the root module I expected the service to be a singleton available to the whole app. Since I use that service to register the icons with iconRegistry.addSvgIconSet()
in my bootstrapped AppComponent
, I expected the icons to be accessible throughout the app.
PS: this may be related to what I reported yesterday, although in that case the app crashes; in this case the icon is just not shown.
(this is a reproduction of my post here because the two issues are really the same)
I figured out that because
MdIconModule
itself hasMdIconRegistry
service in itsproviders
array, every time another module imports it, a new instance of the service is provided. As a result, all components that are loaded at bootstrap time and that belong to the sameAppModule
share the same instance of this service. However, components loaded later (via lazy-loading) have a different instance of the service and as a result cannot see the icons registered at bootstrap time.With help from James, I've used the workaround of a specially crafted module that does not use
MdIconModule
at all, but rather declares theMdIcon
class alone. I then provide theMdIconRegistry
service separately, and only to the rootAppComponent
. The result is that there is only one instance of the service app-wide and icons registered at bootstrap time are available everywhere.Modified MdIconFixedModule
Modules that need to just use icons can import
MdIconFixedModule
as this doesn't contain theMdIconRegistry
. TheAppModule
that also needs to register icons importsMdIconFixedModule.forRoot()
which does contain the service.The details of this implementation can be seen here.
This limitation of ng-Material Modules is slotted to be fixed with the alpha 8 release