简体   繁体   English

在这种情况下,处理AngularJS事件的最佳方法是什么?

[英]What is the best way to handle events in AngularJS in this case?

I am absolutly new in AngularJS and I have the following doubt about how to handle event in Angular. 我在AngularJS中绝对是新的,我对如何处理Angular中的事件有以下疑问。

So I know if in a view I have something like this: 所以我知道如果在某个视图中我有这样的东西:

<input type="text" ng-model="handle" />

it means that exist a 2 way binding between this input element in the dom and an handle variable into the Angular $scope , for example: 它意味着在dom中的这个输入元素和Angular $ scope中的一个handle变量之间存在双向绑定,例如:

$scope.handle = '';

So any change that happen into this input object implies a change of the handle property in the $scope and vice-versa. 因此,发生在此输入对象中的任何更改都意味着更改$ scope中的handle属性,反之亦然。

So, into the Angular applcation, I can explicitly declare a whatcher 因此,在Angular applcation中,我可以明确地声明一个whatcher

// I explicitly declare a whatcher on the handle property: when the value of this propertu change the function() is performed:
$scope.$watch('handle', function(newValue, oldValue) {

    console.info('Changed!');
    console.log('Old:' + oldValue);
    console.log('New:' + newValue);

});

So it should have the same meaning of manually adding a classic vanilla JavaScript EventListener (by the addEventListener() on the object that I want to observe in a classic old vanilla JavaScript application (that don't use Angular). 因此它应该具有相同的含义,即手动添加经典的vanilla JavaScript EventListener(通过addEventListener()在我想要在经典的旧vanilla JavaScript应用程序(不使用Angular)中观察的对象上。

When something change on the input value the function associated to the whatcher is performed. 当某些内容在输入值上发生变化时,将执行与该祭司相关联的功能。

Ok it is pretty clear for me. 好吧,对我来说很清楚。

Now I say that I can also do something like this. 现在我说我也可以这样做。

Into the HTML code I can have something like: 进入HTML代码,我可以有类似的东西:

<input type="button" value="Click Me" ng-click="alertClick()" />

And in the Angular controller I will have something like: 在Angular控制器中,我会有类似的东西:

$scope.alertClick = function() {
    alert("Clicked !!!");
}

So it seems like the ng-Click directive perform a function that is a $scope field when there is the click event on the button. 因此,当按钮上有click事件时,似乎ng-Click指令执行的函数是$ scope字段。

But can I do the same operation as done in the first example? 但是,我可以执行与第一个示例中相同的操作吗? Can I do it declaring a whatcher on the button if it is associated to a model object by the ng-model="handle" directive? 如果它通过ng-model =“handle”指令与模型对象相关联,我可以在按钮上声明一个whatcher吗?

When is better use the first method and when is better the second method to handle events in AngularJS? 何时更好地使用第一种方法,何时更好地处理AngularJS中的事件的第二种方法?

ngModel is used to bind an imput form the UI to your controller ngModel用于将UI的输入绑定到控制器

ngClick is just a regular javascript expression that have access to your controller scope that will be executed at the click event. ngClick只是一个常规的javascript表达式,可以访问将在click事件中执行的控制器作用域。

Here you have to use ng-click. 在这里你必须使用ng-click。

With angular a good practice is to avoid using function like addEventListener() or Jquery other function... Because AngularJS wrap all this functionality and will run digest loop or other voodoo magic if necessary. 使用angular是一个好的做法是避免使用像addEventListener()或Jquery等函数的函数...因为AngularJS包装了所有这些功能,并且如果需要将运行摘要循环或其他巫毒魔法。

Use the click event. 使用click事件。 $scope.$watch should be used watching when something changes instead of things that are better for event handlers. $ scope。$ watch应该用于观察某些内容发生变化而不是更适合事件处理程序的内容。

The tow "methods" you pointed out are not the same thing. 你指出的两种“方法”并不是一回事。 At all. 完全没有。

The first, $watch, is intended to listen to the $scope changes. 第一个是$ watch,用于收听$ scope更改。 You can specify which property of the scope you want to watch, and when a change occur it will call you callback function. 您可以指定要监视的范围的哪个属性,以及何时发生更改,它将调用您的回调函数。 For more details, see the Digest cycle documentation . 有关更多详细信息,请参阅摘要周期文档

The second, ng-click attribute directive, listen to the DOM events and evaluate the expression you pass in when the event occur. 第二个ng-click属性指令,监听DOM事件并评估事件发生时传入的表达式。

In your case, for the button, you have two options. 在您的情况下,对于按钮,您有两个选项。 You can use the ng-click attribute directive to trigger a function in your scope OR use the ng-submit attribute directive in the form html tag of your inputs. 您可以使用ng-click属性指令触发范围中的函数, 或者在输入的html标记中使用ng-submit属性指令。 That way you can trigger the form validation with the button or when the Enter is pressed. 这样,您可以使用按钮或按下Enter键来触发表单验证。

See the documentation for ngSubmit . 请参阅ngSubmit的文档。

ngModel applies to specific elements such as input,select, and textarea. ngModel适用于特定元素,如input,select和textarea。 As the result, you cannot use ngModel on a button. 因此,您无法在按钮上使用ngModel。 That is why you use ngClick to get the click event. 这就是您使用ngClick获取click事件的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Javascript中处理多个关键事件的最佳方法是什么? - What is the best way to handle multiple key events in Javascript? 在开发过程中处理虚假拖放事件的最佳方法是什么? - What is the best way to handle spurious drag/drop events during development? 处理多次点击事件的最佳方法 - Best way to handle multiple click events 使用jQuery处理移动设备上的长按和双击事件的最佳方法是什么? - What's the best way to handle longtap and double-tap events on mobile devices using jQuery? 在angularjs中处理重复工厂的正确方法是什么? - What is the right way to handle duplicated factory in angularjs? 处理会话超时的最佳方法是什么? - What's the best way to handle a session timeout? 处理承诺错误的最佳方法是什么? - What is the best way to handle errors for promises? 用 - 和 处理数字的最佳方法是什么? 带有受控输入? - What is the best way to handle numbers with - and . with a controlled input? 在 Javascript 中处理分页 API 的最佳方法是什么? - What is the best way to handle paginated API in Javascript? 处理网站上的模式弹出窗口的最佳方法是什么? - What is the best way to handle modal popups on a website?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM