我阅读 Angular 教程时,很喜欢其中的许多内容,但是 "ng-click" 不是相当于内联 onClick 吗?我的理解是 JavaScript 社区已经确定在 HTML 中使用内联 JavaScript 事件处理程序是“不良实践”,对吗?
<img ng-src="{{img}}" ng-click="setImage(img)">
了解为什么在使用 Angular 时这不再被视为“不正确”将会很有帮助。
我阅读 Angular 教程时,很喜欢其中的许多内容,但是 "ng-click" 不是相当于内联 onClick 吗?我的理解是 JavaScript 社区已经确定在 HTML 中使用内联 JavaScript 事件处理程序是“不良实践”,对吗?
<img ng-src="{{img}}" ng-click="setImage(img)">
了解为什么在使用 Angular 时这不再被视为“不正确”将会很有帮助。
实际上,一切都归结于你的视图代码必须以某种方式与应用程序逻辑相连接。 AngularJS 的最佳实践通常建议您编写自己的模型——代表业务领域的对象——并将它们附加到作用域上。想象一下类似这样的代码:
<img ng-src="{{img}}" ng-click="myProfile.setMainImage(img)">
myApp.controller("ProfileController", function($scope, myProfile) {
$scope.myProfile = myProfile;
});
$("#someId img").on('click', function() {
var img = $(this).attr('src');
myProfile.setMainImage(img); // where does myProfile come from here?
// how does it update the view?
});
是我自己的问题还是AngularJS看起来不太好?Ryan,别误会,这一集很棒,但是我对这个框架并不信服。
所有的ng-show
,ng-repeat
和ng-class
看起来像旧的Java JSF和类似的框架。它还通过ng-submit
和ng-click
强制执行显眼的JS。
所以我的观点是:你的视图很容易变得混乱,并且完全依赖于它。其他框架(如Backbone)的优点在于展示和行为之间有关注点分离(更少或没有依赖项),以及结构化的客户端应用程序(MVVM)。
我的回复在这里也适用:
(注:实际上,Angular版本应该是In a framework like Backbone, you'd have something like the following code (taken from the Backbone website, minus a few lines):
var DocumentView = Backbone.View.extend({ events: { "dblclick" : "open", "click .icon.doc" : "select", "contextmenu .icon.doc" : "showMenu", "click .show_notes" : "toggleNotes", "click .title .lock" : "editAccessLevel", "mouseover .title .date" : "showTooltip" }, open: function() { window.open(this.model.get("viewer_url")); }, select: function() { this.model.set({selected: true}); }, });
In this object which is a view, you are setting up event handlers on various elements. These event handlers call functions on the view object, which delegate to models. You also set up callbacks on various model events (such as
change
) which in turn call functions on the view object to update the view accordingly.In Angular, the DOM is your view. When using
ng-click
,ng-submit
, etc., you are setting up event handlers on these elements, which call functions that should delegate to model objects. When usingng-show
,ng-repeat
, etc. you are setting up callbacks on model events that change the view.The fact that AngularJS sets up these [hooks and] callbacks behind the scenes for you is irrelevant; the only difference between this and something like Backbone is that Angular lets you write your view declaratively--you describe what your view is--rather than imperatively--describing what your view does.
So, in the end,
<a ng-click="model.set({selected: true})">
really adds no more dependencies than
events: { 'click a': 'select' }, select: function() { this.model.set({selected: true}); }
...but it sure is a hell of a lot less code. ;)
<a ng-click="select()">
,并且作用域中的select
方法类似于Backbone示例中视图中的select
方法。)ng-click
附加到它们所有元素中。我感觉过去5年里我学习了像这样的内联javascript(不要试图说它不是-您实际上正在从HTML元素定义中调用方法 select()
)是不好的做法,只是为了被告知“只要是JavaScript框架鼓励使用就可以”。这感觉很奇怪。关注点分离是怎么了? - nzifnabng-click
等是对非全局对象($scope
)上的方法的绑定,以明显和声明性的方式编写。(通过指令也可以帮助复制/粘贴问题。) - Michelle Tilley