简体   繁体   English

直接从 angular 模板更改变量值是一种好习惯吗

[英]Is it a good practice to change variable value directly from angular template

I have a variable in the component:我在组件中有一个变量:

showModal: boolean

And this in the template:这在模板中:

<div (mouseover)="handleHover('hover')">

handleHover() changes the value of showModal . handleHover()更改showModal的值。

Should I use a handleHover() type func and point (mouseover) to it or is it fine to do something like this?我应该使用handleHover()类型的函数并指向(mouseover)它还是可以做这样的事情?

<div (mouseover)="this.showModal = true"">

When it comes to angular it's better to keep the template simpler as much as possible if you are following the template-driven-forms approach.当谈到 angular 时,如果您遵循template-driven-forms方法,最好使模板尽可能简单。 I assume you are following the template-driven forms approach.我假设您正在遵循模板驱动的 forms 方法。 Therefore it's better to do the assignment via the component itself.因此,最好通过组件本身进行分配。

Either way is fine but for the sake of consistency and clean maintainable code, I would encourage you to follow handleHover() type function and point (mouseover) event to the particular function and do the rest.两种方式都可以,但为了一致性和干净的可维护代码,我鼓励您遵循handleHover()类型 function 并将(mouseover)事件指向特定的 function 并执行 rest。

For further clarity on these Angular issues I would strongly recommend following the A ngular Style Guide best practices mentioned in the Angular Style Guide in Official Angular Documentation here .为了进一步阐明这些 Angular 问题,我强烈建议您遵循官方 Angular 文档中Angular 风格指南中提到的Angular 风格指南最佳实践。

Personally I would argue it is fine, and even preferable, to assign directly to the showModal variable in the template in the case that the only purpose of handleHover() is to set the variable showModal .我个人认为,在handleHover()的唯一目的是设置变量showModal的情况下,直接分配给模板中的showModal变量是很好的,甚至更好。

When reading the template, it is clear to see that only one simple assignment takes place, whereas using a function introduces the small overhead of needing to consult the .ts file to understand what is happening.阅读模板时,可以清楚地看到只发生了一个简单的分配,而使用 function 引入了需要查阅.ts文件以了解正在发生的事情的小开销。 It also then completely bypasses the need to have handleHover() at all (imagine how this would scale if you needed handlePrimaryBtnHover() , handleSecondaryBtnHover() , handleSecondaryBtnClick() etc if all these functions are doing is updating a single variable).它还完全绕过了handleHover()的需要(想象一下,如果您需要handlePrimaryBtnHover()handleSecondaryBtnHover()handleSecondaryBtnClick()等,如果所有这些功能都在更新单个变量,这将如何扩展)。

This is minor though and the handleHover() approach is definitely not bad of course.虽然这是次要的,但handleHover()方法当然也不错。

I would say that handleHover() may be preferred when我会说handleHover()可能是首选时

  • It contains >1 lines of code, eg它包含> 1行代码,例如
handleHover() {
  this.showModal = true;
  this.showModalSubject.next(this.showModal)
}
  • The function needs to be referenced in many different places in the same template (although I still think showModal = true is more readable/succint for this use case) function 需要在同一个模板的许多不同地方被引用(尽管我仍然认为showModal = true对于这个用例来说更具可读性/简洁性)
  • You want to explicitly test a TypeScript method that handles the assignment of showModal您想要显式测试处理showModal分配的 TypeScript 方法

Note: you do not need to include this when referencing variables from the template注意:从模板中引用变量时不需要包含this

<div (mouseover)="showModal = true">

Using a function in your component to handle the event and update the variable, like handleHover() , is a good practice because it keeps the component's template simple also for reusability, you can use the same handler again and it's better for testing在你的组件中使用function来处理事件和更新变量,比如handleHover() ,这是一个很好的做法,因为它保持组件的模板简单,也为了可重用性,你可以再次使用相同的处理程序,它更适合测试

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM