Angular 6 private methods

百般思念 提交于 2019-12-04 02:43:32

In Angular we have 2 models of compilation

  • JIT - Just-in-Time Compilation : JIT compilation as the name implies, compiles the application Just-in-Time in the browser at runtime.

  • AoT - Ahead-of-Time Compilation : AoT compilation compiles the application at build time.

By default, with the development build i.e ng serve we get JIT compilation. This is how it works. The application code along with the angular compiler is downloaded by the browser. At runtime, when a request is issued to the application, the JIT-compiler in the browser compiles the application code before it is executed.

with the production build i.e ng build --prod we get AoT compilation the angular application is pre-compiled. So this means the browser loads executable code so it can render the application immediately, without waiting to compile the application first.

TypeScript public doesn't matter but private does

From Angular Docs
All data bound properties must be TypeScript public properties. Angular never binds to a TypeScript private property.

Actually, it does bind to the private properties, but not in AoT mode

Why AOT Compiler requires public properties, while non-AOT allows private properties?

With JIT we convert all the code to ES5 and then at runtime, we do the bindings. All the visibility modifiers are lost in that process, so it doesn't matter if you say public or private for that.

On the other hand, with AoT, we generate some typescript code for our templates, that will try to access those fields. If they are private, they simply cannot access those properties, hence, you need to put them as public.

Properites used in templaes must be public - this is connected with AoT compilation.

https://angular.io/guide/aot-compiler (find word "public" there)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!