简体   繁体   English

Angular 更改检测,当从模板调用 function

[英]Angular change detection, when calling a function from template

I have just called a function from a template and inside that I am simply returning true or false;我刚刚从模板中调用了 function,在里面我只是返回真或假;

https://stackblitz.com/edit/angular-ivy-awrmsl?file=src%2Fapp%2Fapp.component.ts https://stackblitz.com/edit/angular-ivy-awrmsl?file=src%2Fapp%2Fapp.component.ts

Check the console of above link project, so array has 7 items but in console I can see twice logs.检查上面链接项目的控制台,所以数组有 7 个项目,但在控制台中我可以看到两次日志。 ( i have updated this question and stackblitz link) i have images and i want on error img tag should not be there so i called the same function twice from template whats correct ways to implement this type of logic? (我已经更新了这个问题和stackblitz链接)我有图像,我想要错误img标签不应该在那里所以我从模板中两次调用相同的function 什么是实现这种类型逻辑的正确方法? i need if error is there in image than no image tag should be there我需要图像中是否存在错误而不是图像标签不应该存在

Please check console logs .请检查控制台日志

I have not changed any property - what might be happening here?我没有更改任何属性 - 这里可能会发生什么?

(I tried different answers in a search engine search still not getting the exact point). (我在搜索引擎搜索中尝试了不同的答案,但仍然没有得到确切的答案)。

In angular, change detection is done by zone.js.在 angular 中,变化检测由 zone.js 完成。 Which figures out what part of the UI need to be re-rendered.它确定了 UI 的哪一部分需要重新渲染。

It will see your method checkimage , since it can't predict the value the method it will execute it.它将看到您的方法checkimage ,因为它无法预测它将执行它的方法的值。 Hence you get multiple logs in the console.因此,您会在控制台中获得多个日志。

Depending on the code inside the checkimage method it can cause performance issues.根据checkimage方法中的代码,它可能会导致性能问题。 Rule of thumb: Don't call functions from the template.经验法则:不要从模板调用函数。

You can change the code simply您可以简单地更改代码

<ng-container *ngIf="id.sr">
  ff {{i}}
  {{  id.sr }}
</ng-container>

ngDoCheck is lifecycle hook provided by angular .This method is used to detect and act upon changes that Angular can't or won't detect on its own. ngDoCheckangular提供的lifecycle hook 。此方法用于检测和处理 Angular 无法或不会自行检测的更改。

This method runs after the ngOnInit once and also after each change detection cycle .此方法在ngOnInit之后运行一次,也在每个change detection cycle之后运行。

In the development mode, angular runs the extra change detection cycle initially.在开发模式下,angular 初始运行额外变化检测周期。

In the production, you would only see one log in your case.在生产中,您只会在您的案例中看到一个日志。

More here更多在这里

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

相关问题 调用函数角度时更改参数的值 - change value of params when calling function angular 模板渲染后的角度变化检测 - Angular change detection after template render AngularDart 4-5更改检测与Angular 5更改检测有何不同? - How AngularDart 4-5 change detection is different from Angular 5 change detection? 递归函数的Angular 2变化检测问题 - Angular 2 change detection issue with recursive function Angular 2中用于onPush更改检测的自定义功能 - Custom function for onPush change detection in Angular 2 为什么从模板或观察者调用时这个 scope 会发生变化? - Why does the this scope change when calling from a template or watcher? 发生 ng-change 事件时,函数未在 angular js 中调用 - Function is not calling in angular js when ng-change event occurs 使用模板文字中的参数调用函数时出现 JS 错误 - JS Error when calling function with argument from Template Literal Angular 对用于属性绑定的模板参考变量的更改检测 - Angular change detection on Template Reference Variables for property binding Angular 6 UI 在不使用父模板的情况下实现更改检测 - Angular 6 UI implementing change detection without using parent template
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM