4 ms·
looks nice, but one thing I really don't get is how <a ng-click="modal.saved=true;dismiss()">...</a> is any different to <a onClick="thisIsAnAntiPatt
by phpnode 14y ago
looks nice, but one thing I really don't get is how
<a ng-click="modal.saved=true;dismiss()">...</a>
is any different to
<a onClick="thisIsAnAntiPattern()">...</a>
I'm all for declarative code but isn't stuffing javascript in html attributes something we learnt not to do about 10 years ago? Why the resurgence?
- randall 14y agoI think as one uses Angular more, a person will move most of their code out of `ng-click` style directives into their own components which might include click handlers on them. That's the case, for me anyway.
- manojlds 14y agoDidn't we learn that static files for serving websites are so uncool many many years ago and stuffed the content into databases and served off them? And what is seeing a great resurgence these days? More things change, more they remain the same. PS: I am not saying JS in attributes is a good thing, but just that these things keep happening, for good or worse.
- marknutter 14y ago> I'm all for declarative code but isn't stuffing javascript in html attributes something we learnt not to do about 10 years ago? And why?
- mcgwiz 14y agoHarder to test, of course.
- Guillaume86 14y agoA good thing that AngularJS is test friendly then
- davej 14y agoonClick will be evaluated within the global scope, ng-click is evaluated within the scope of the controller. That's a pretty substantial difference. Your HTML has to be hooked up to your controller somehow, I personally prefer angular's declarative approach rather than a big messy init method.
- JanLaussmann 14y agoAn interesting discussion about the difference between <a ng-click="modal.saved=true;dismiss()">...</a> and <a onClick="thisIsAnAntiPattern()">...</a> and why context / scope matters could be found here: http://www.bennadel.com/blog/2439-My-Experience-With-AngularJS-The-Super-heroic-JavaScript-MVW-Framework.htm#comments_41586 http://www.bennadel.com/blog/2439-My-Experience-With-Angular...
- phpnode 14y agothanks, that was interesting and it's obvious how it can be useful. but for some reason it still feels dirty.
- evilduck 14y agoI'm pretty sure the linked site is using code inside ng-click directives for illustrative purposes. It makes documenting and demonstrating the project much easier.
- bemurphy 14y agoThis is a bit tangent to your question, but, I think you can still run with ng-click if you deploy Content Security Policy, whereas onclicks will be banned.
- xaritas 14y agoThis is an example for brevity but certainly misleading if you're not familiar with Angular. You're right that this is typically an anti-pattern, but it's one that Angular tries to eliminate, even though it is using a similar looking mechanism. Angular controllers expose an API for the widget they enclose, but it leaves the wiring up to the designer. Sometimes it makes sense to sprinkle event bindings onto the DOM via code, but there are many cases where it doesn't. My code almost never needs to know exactly what element(s) are bound to a method, so it makes sense to me to just declare that stuff right in the document. As a side point, you can't evaluate arbitrary JavaScript in Angular attributes. Angular parses the attributes and evaluates them as in a JavaScript-like way, but with some exceptions. See http://docs.angularjs.org/guide/expression http://docs.angularjs.org/guide/expression