In the first part of this post we ended up with a simple but repetitive solution to wrapping Angular Material button in a custom component. In this...
For further actions, you may consider blocking this person and/or reporting abuse
Hi Dzhavat,
Thanks a lot for this blog post and the idea behind it.
I think it's a good approach, but I wondere how you would manage some important properties for buttons, like disabled and type ('submit').
When we use ngComponentOutlet we can't use input. Maybe we would have to create other types of components, for submit it could be fine, but for disabled, which is supposed to be dynamic, it might be harder.
Hi Benoît,
Thanks for you comment.
We're using this approach on a button component at work. We recently had a use case where we had to change the type of one button to
submitand another one tobutton. What we did was we introduced a new property. We changed thetypeproperty to accept a type (i.e.submitorbutton,buttonbeing the default) and we added avariantwhich could beprimary,secondaryortext.As for the disabled attribute, the
ngComponentOutlethas aninjectorinput which can be used to inject values. How we use it is to provide a custom token, which is also injected in the internal components as well. This way it's possible to pass dynamic values. If you're interested in knowing more, I can point you to some examples. Hope some of this helps you :)Hi Dzhavat,
Thanks a lot for your reply. Yes I learnt than we can use an
injectorin thengComponentOutlet paramsbut I didn't succeed making it work yet. If you have an example, it would be helpful :-)Sure :)
In this code you can see how we create the
injectorwhich is later passed tongComponentOutletin the template. We providedisabledAttributeTokenwhich is injected in all internal buttons to control the disabled state. The same way we inject thetypeattribute.Hi Dzhavat,
Thanks a lot for this example ! It will help a lot, hopefully not only me :-)
Have a nice day !
Hi Dzhavat,
I could successfully implement my version on your button ;-)
I faced a problem regarding the disabled property. It works well on instanciated buttons, but, when listening to click event on the wrapper, you still get the event, even if the button is disabled.
I tried to preventDefault/stopPropagation the click event from the Host but it didn't work.
Finally I did this :
I don't know if you faced that problem yet :-)
Learned some good stuff in these two posts - thanks!
Thanks Dave. I'm glad to hear that 😊
Other parts will be coming as well :)
Cool, I look forward to it. ✌🏻