Resources in Angular 4 (and 5) are scarce. Really. Sometimes I get so lost in sifting through Stack Overflow that I end up translating the answers ...
For further actions, you may consider blocking this person and/or reporting abuse
Two things I'd like to point out:
Hi, Matteo! I am not - by any means - an expert in Angular but I just want to help other developers who may be lost right now looking for solutions by sharing mine. :-) Thanks for these pointers!
This was exactly what I was looking for! Much appreciated.
Same over here... :)
In this case you've missed
$event.stopPropagation()
, which could be important in some cases.So, method still can make sense in this case if you'd like to keep your html clean.
Mine resulted to 'ERROR TypeError: Cannot read property 'click' of undefined'. Did I missed out something?
Hello, sadly i switched to Vue a long time ago so i may be a bit behind current Angular, but if you get that error it appears that you forgot to add to the input the reference attribute
#photoInput
.Just tested it here and the code appears to be working as originally wrote:
stackblitz.com/edit/angular-playgr... ( checkout hello-framework/hello-framework.component.html )
Hi matteo may I ask if this will work when you use #templateRef using material UI in angular?
Hi Monique, how do you unit-test the example? I have created a similar application but I am unable to unit-test it as I don't know how to trigger the
change
event from file selector. I have created a question in StackOverflow. Would it be possible for you to take a look at stackoverflow.com/questions/542022...I like this sample more or less cuz the element-id could be used in a dynamical way. i.e. if I don't know the id at the codeing time but only on runtime I can still use the given approach. Which I like :-)
Instead of getElementById there's a good way of controlling the elements on the template by using the @ViewChild Directive. (Angular 8)
in the template adding a local reference to the button: (#photoInputButton)
in the component
adding the @ViewChild directive referencing the photoInputButton:
@ViewChild('photoInputButton', {static: false}) photoInputButton: ElementRef;
//{static: true} => only if it's being used in the ngOnInit phase
Now you have an ElementRef - you can trigger the click:
this.photoInputButton.nativeElement.dispatchEvent(new MouseEvent('click'));
Need 1 help to understand guys.
When Angular suggests(I guess so) to use Renderer2 and not directly access your HTML DOM, doesn't that future proof law or whatever we call that, applies to this code.
Is there any other way if we had to do it reactively and not template wise, at least add some custom code in between like "event.preventDefault()"
It's nice to see fellow Filipinos active in this community!
Hi!
I don't know if this is still active but I have some questions.
I've tried your approach above and also the one Matteo Rigon provided. Both of them resulted to 'ERROR TypeError: Cannot read property 'click' of undefined'. Did I missed out something before applying these approaches to trigger click event? I'm using Angular 7 by the way.
Thanks
I found a solution that is more elegant, standard, degrades gracefully and is framework-agnostic. Check it out here: tympanus.net/codrops/2015/09/15/st...
This is great! Thanks :-)
Thank you Monique, it was very useful for me!
great post it really helped me, thanku
Hi Monique.nice article.if i have to trigger a component(that is a form) through button click ,what is the syntax
Woww. great trigger
Hey , Nice article..
Does this work in angular 7 and above ?
i want to trigger an event from another element interaction
I haven't tried, but it should 🙂
Thank you very much @gloriamaris . I searched a lot about how to implement this, but your post was very helpful. Thanks. ❤️