In angularjs there are examples where the ng-click handler is attached directly to an html element such as <a>
or <button>
. See my example below (taken from an angular home page example). It defines the anchor tag's click handler, which will call archive()
.
Doesn't this go against the whole MV* framework? Wouldn't it be better to define the click somewhere other than the view? It just seems like a bit too much functionality is now attached to the anchor tag. Can anyone enlighten me here?
Example Code:
<a href="" ng-click="archive()">archive</a>
Well, you do have to attach commands / behaviours somewhere and I think Angular's approach to do it declaratively in the view is much nicer than doing in imperatively in JS files with DOM selectors which will silently break as soon as you change your markup structure / naming.
This is what Microsoft's XAML / C# MVVM framework does too and the separation of concerns is beautiful there, I'm really hoping Angular is going to go towards that model where it's possible to encapsulate and attach view only behaviours purely in markup.