Az AngularJS (1.x verzió) egy JavaScript alapú nyílt forráskódú keretrendszer. Ez több platformon használható, és egyoldalas webalkalmazások (SPWA) fejlesztésére szolgál. Az AngularJS az MVC mintát valósítja meg a logika, a prezentáció és az adatkomponensek elválasztására. Függőség-injektálást is használ a kiszolgálóoldali szolgáltatások használatához kliensoldali alkalmazásokban.
Az Angular (2.x és újabb verziók) egy Typescript alapú nyílt forráskódú keretrendszer front-end webalkalmazások fejlesztésére. Az Angular olyan funkciókkal rendelkezik, mint a generikus, a statikus gépelés és az ES6 néhány funkciója.
Javasoljuk az Angular elsajátítását és új projektekhez történő felhasználását. Az AngularJS-t főleg régi projektekhez használják.
Az Angular megtanulásának legjobb módja a freeCodeCamp 6 órás szögletes oktatóprogramja a YouTube-on.

Egyéb oktatóanyagok az Angularról
Szögletes 1.x
Általános oldalak
- Szögletes JS - A szögletes JS honlap
- AngularJS Stíluskalauz - Részletes bevált gyakorlatok a szögfejlesztéshez
Videók
- Útvonal szögletes JS-ben - Ügyféloldali útvonal 15 perc alatt
- Angular ToDo App - Szögletes ToDo alkalmazás 12 perc alatt
Tanfolyamok
- Egghead.io AngularJS tanfolyamok ($)
Szögletes 2.x +
Általános oldalak
- Szögletes - A szögletes honlap
- Angular Style Guide - Részletes bevált gyakorlatok a szögfejlesztéshez
Specifikus témájú oldalak
- Irányelvek - Kiváló útmutató a szögirányelvek részletezéséhez (1. rész)
Tanfolyamok
- Egghead.io szögletes tanfolyamok ($)
- FrontendMasters - Awesomer alkalmazások építése szögletes
- Végső szögletes - Todd mottó
- Angular 6 (korábban Angular 2) - A teljes útmutató ($) Maximilian Schwarzmüller
Blogok
- Alligator.io
- Szögletes mélységben
Verziótörténet
A Google 2010. október 20-án adta ki az AngularJS eredeti verzióját. Az AngularJS első stabil kiadása 2017. december 18-án volt az 1.6.8 verzióról. Az Angular 2.0 kiadásra 2014. szeptember 22-én került sor az ng-Europe konferencián. Az Angular 2.0 egyik jellemzője a dinamikus terhelés.
Néhány módosítás után az Angular 4.0 2016 decemberében jelent meg. Az Angular 4 visszamenőleg kompatibilis az Angular 2.0-val. A HttpClient könyvtár az Angular 4.0 egyik jellemzője. Az Angular 5 2017. november 1-én jelent meg. A progresszív webalkalmazások támogatása az Angular 4.0 egyik fejlesztése volt. Az Angular 6 2018. májusában jelent meg. A legújabb stabil verzió a 6.1.9
Telepítés :
Az Angular-t hozzáadhatjuk a rendelkezésre álló források hivatkozásával vagy a keretrendszer letöltésével.
Link a forráshoz :
AngularJS: Hozzáadhatjuk az AngularJS-t (Angular 1.x verziók) a Google Tartalom-szállítási hálózatára hivatkozva.
Letöltés / telepítés: Letölthetjük a keretrendszert az npm, a Bower vagy a zeneszerző segítségével.
1.x szög :
npm
npm install angular
Ezután adjunk hozzá egy a
index.html
:
lugas
bower install angular
Ezután adjunk hozzá egy a
index.html
:
A dokumentációval kapcsolatos további információk az AngularJS hivatalos webhelyén találhatók.
Az Angular 2.x és más verzióit telepítheti az Angular hivatalos dokumentációjában szereplő lépések végrehajtásával.
Alkatrészek
Motiváció
Az Angular számos vázlatot tartalmaz az építési alkalmazásokhoz. Az alkatrészek ilyen vázlatok. Egyetlen logikai egységet ölelnek fel, amely az alkalmazás egyetlen részével foglalkozik. Az alkatrészek a hatékonyabb működés érdekében gyakran társulnak más sémákkal.
Az összes vázlat között az alkatrészek általában többet fogyasztanak, mint amennyit nyújtanak. Míg más vázlatok, például irányelvek, csövek és szolgáltatások nyújtanak hasznosságot, az alkatrészek felhasználják. Ők felelősek az alkalmazás felületéért, ezért van értelme miért fogyasztanak hasznosságot.
Az alkatrészek egyszerűsítik az alkalmazást. Elsődleges céljuk, hogy a logikát a látható felület egyetlen szakaszába továbbítsák. Az alkalmazások lépésről lépésre történő összeállításához komponensenként kell felépítenie. Az alkatrészek végül is Angular építőelemeként működnek.
Összetevők Bevezetés
Mint már említettük, az összetevők felhasználják a segédprogramot (szolgáltatásokat / erőforrásokat). Az üzleti logika és a prezentáció között állnak, hogy összetartó egységet hozzanak létre. A szögletes különféle mechanizmusokat köt minden alkatrészhez. Ezek a mellékletek az osztályt összetevőként azonosítják, és meghatározzák annak szabványos képességeit.
A szögletesnek fel kell ismernie az alkatrészeket, amikor rájuk kerül. Ehhez @Component
minden egyes osztályt fel kell díszítenie, amelynek alkatrésznek szánják. A díszítők jelzik az Angular számára, hogy mi az osztály.
Egy alkatrész esetében tudnia kell, hogyan kell interakcióba lépni az injektorával, csatlakozni egy sablonhoz, kivonni a stílusok listájából, beilleszteni a stílusokat stb. Az Angular gondoskodik a legtöbb alacsony szintű követelményről. A fejlesztőknek még mindig konfigurálniuk kell az összetevő viselkedését, importálniuk kell a függőségeket és bővíteniük kell a logikát.
Minden ilyen dologhoz megvan a komponens osztálya. Az osztály mindent viszonylag egységesen tart. Összefoglalja a komponens üzleti logikáját.
Alkatrészosztály és metaadatok
Go ahead and install the Angular command-line interface (CLI). You can learn more about it from this article. The CLI command ng generate component [name-of-component]
yields the following.
import { Component, OnInit } from '@angular/core'; @Component({ selector: 'app-example', templateUrl: './example.component.html', styleUrls: ['./example.component.css'] }) export class ExampleComponent implements OnInit { constructor() { } ngOnInit() { } }
This is the basic skeleton from which all great components originate. The @Component
decorator is the most important part. Without it, the above example becomes a generic class. Angular relies on decorators to discern a class’s schematic type.
@Component
receives metadata as a single object. Decorators are just JavaScript functions under the hood. They take in arguments as with the metadata object. The metadata object configures a component’s basic dependencies. Each fields plays a role.
selector:
tells Angular to associate the component with a certain element in the application’s template HTML.templateUrl:
accepts the file location of the component’s template HTML (this is where data gets displayed to).styleUrls:
accepts an array of style-sheet file locations (strings). These style-sheets target the component’s assigned template.
Think of metadata as a big blob of configuration. The decorator takes it so that it can generate the data specific to the component. The decorator decorates the underlying class with data necessary for its class’s behavior. A component class that is.
The class’s signature exports by default so that the component can be imported. ngOnInit
also gets implemented. implements
tells the class to define certain methods per the interface’s definition. ngOnInit
is a lifecycle hook.
Component Lifecycle and Change Detection
Components use all sorts of tools, services, and features. One key feature available to components is lifecycle hooks. An explanation for each hook exists in this article.
There are eight in total and they all serve as timing functions. They execute conditionally as the component transitions from state-to-state via change detection. This process happens constantly across the component tree. It searches for changes in data which merit a re-rendering of the template.
Time to move on. Please refer to the aforementioned articles for more information on the component lifecycle. It deserves much more explanation.
Component Data
Data drives everything. Components are no exception. Components encapsulate all their data. To receive data externally, a component must explicitly declare it. This form of privacy keeps information from clashing across the component tree.
Data determines what gets displayed from the component class to its template. Any updates to the class’s data will (or at least should) update the template display.
Components will often initialize a set of members (or variables) that store data. They are used throughout the component class logic for convenience. This information fuels the logic resulting in the template and its behavior. See the following example.
// ./components/example/example.component.ts import { Component, OnInit } from '@angular/core'; import { Post, DATA } from '../../data/posts.data'; @Component({ selector: 'app-example', templateUrl: './example.component.html' }) export class ExampleComponent implements OnInit { username: string; totalPosts: number; allPosts: Post[]; deletePost(index: number): void { this.allPosts.splice(index, 1); this.totalPosts = this.allPosts.length; } ngOnInit(): void { this.username = DATA.author; this.totalPosts = DATA.thePosts.length; this.allPosts = DATA.thePosts; } }
{{ username }}
Change Name: Posts: {{ totalPosts }}
DELETE
{{ post.title }}
{{ post.body }}
Note the ways the component interacts with its data. It first fetches it from ../../data/posts.data
before it begins to forward it to the template for display.
The data shows up throughout the template. Inside the double curly braces, a variable’s value is mapped from the component class into the braces. The *ngFor
loops across the allPosts
class array. Clicking on the button removes a specific element from allPosts
by its index. You can even change the topmost username
by typing into the input box.
The above interactions alter the component class’s data which in turn updates the component’s template HTML. Components provide the backbone logic that facilitates the flow of data. The template HTML makes that data readable to the user.
Component Template
The previous example’s template HTML featured an interesting syntax. The syntax was not actual HTML. It was Angular’s template HTML. Some often refer to it as HTML Plus, recognizable only by Angular’s compiler. The compiler supports a syntax resulting in the dynamic manipulation of HTML. This article will often refer to it as ‘template HTML’ or ‘template’.
The syntax lets components inject data directly into the template HTML. The injection is dynamic. This means that data can iterate and display itself as HTML without needing external assistance. The Angular compiler compiles it into real HTML by the time it reaches the web browser.
To learn more about some of the ways data binds to the template, read about data binding in Angular. A few examples of data binding occurred in the previous example ({{ ... }}
). For this article, it is enough to recognize data interactions were happening between the component class and its template.
Querying the Template
Data managing the state of the template imperatively works OK. Yet, pure data does not always fulfill an application’s intended design. Interacting more directly with the Document Object Model (DOM) may be required.
To do that, the component must have reference to the template elements. When the data changes, the component can manipulate the DOM explicitly. This is a more declarative approach.
Components can grab references using a web browser’s DOM application programming interface (API). Bad idea though. Angular prefers cross-platform compatibility. For a component to function outside of the web browser, it needs to use Angular’s API instead of the DOM’s.
Components can query their templates using the @ViewChild
and ContentChild
decorators. They grab references to template elements on behalf of the component class.
import { Component, ViewChild, ContentChild, ElementRef, Renderer2, AfterContentChecked, AfterViewChecked } from '@angular/core'; @Component({ selector: 'app-child', template: ` Toggle Enlarge ` }) export class ChildComponent implements AfterContentChecked { @ContentChild("pReference", { read: ElementRef }) pElement: ElementRef; textEnlarge: boolean = false; constructor(private renderer: Renderer2) { } toggleEnlarge() { this.textEnlarge = !this.textEnlarge; } ngAfterContentChecked() { if (this.textEnlarge) this.renderer.setStyle(this.pElement.nativeElement, 'font-size', '25px'); else this.renderer.setStyle(this.pElement.nativeElement, 'font-size', 'initial'); } } @Component({ selector: 'app-parent', template: ` Toggle Highlight View Child
Content Child
` }) export class ParentComponent implements AfterViewChecked { @ViewChild("hOneRefereance", { read: ElementRef }) hOneElement: ElementRef; textHighlight: boolean = false; constructor(private renderer: Renderer2) { } toggleHighlight() { this.textHighlight = !this.textHighlight; } ngAfterViewChecked() { if (this.textHighlight) this.renderer.setStyle(this.hOneElement.nativeElement, 'background-color', 'yellow'); else this.renderer.setStyle(this.hOneElement.nativeElement, 'background-color', 'initial'); } }
The above example contains two buttons that toggle a certain style for each element. Clicking the buttons toggles the true/false values unique to each component. These booleans determine if the custom styles apply. Instead of these values causing changes imperatively, the lifecycle hooks (ngAfterViewChecked
and ngAfterContentChecked
) declaratively alter the DOM.
The declarative approach explicitly changes the style through the element’s reference. In imperative programming, changes to the DOM based off data are implicit. Check out this article on imperative and declarative programming to learn more.
The main thing to notice is how these references get pulled from the template. In the example, there are two sections of the template queried using two decorators: @ViewChild
and @ContentChild
.
They differ in where they look for an element’s reference whether it be in the content DOM or view DOM. These two DOMs exist in ParentComponent’s template. Differentiating between them is important because they finish rendering at separate times.
This is why @ViewChild
and @ContentChild
both exist. They work together with their companion lifecycle hooks ngAfterViewChecked
and ngAfterContentChecked
. These lifecycle hooks wait for their respective queries to resolve before executing.
Once resolved, @ViewChild
and @ContentChild
provide references to two elements. Both exist in separate parts of the DOM. The boolean data still determines the outcome. How that outcome translates to the DOM is the key difference from before. The DOM updates viaRenderer2
’s direct manipulation of it.
Content Projection
The content DOM exists in the innerHTML of ChildComponent’s element. It is all positioned within ParentComponent’s template. The innerHTML of
app-child
projects onto ChildComponent’s template through .
This exemplifies content projection. Displaying content from one
component to another using the innerHTML of another
’s tags in one
’s template so that another
component can pull that innerHTML into its own template via . Thank you for reading that sentence.
Hence why ChildComponent references its element using
@ContentChild
. Content contained within in ParentComponent’s template makes up the content DOM. ChildComponent references the element with an
@ContentChild
query.
ParentComponent’s view DOM consists of everything accessible from within the component’s view. This does not necessarily include the entire template given the innerHTML of . Again, this part of the DOM is queried from ChildComponent using
@ContentChild
. Everything else gets queried using @ViewChild
from the ParentComponent class.
This is a great way for components to exchange content and query their own content regardless of their DOM type. Components can communicate with themselves and others using data binding as well. Read more about it from this article.
Component Styles
Styles are critical to a component’s readability and interactivity. Each component encapsulates its style-sheet dependencies. That way they only apply to the component’s template HTML. A special technique introduced by HTML’s shadow DOM makes this possible.
A shadow DOM branch may exist on any element. This part of the DOM cannot be seen from the HTML’s source code. Standard HTML elements leverage the shadow DOM to provide their trademark appearances. A shadow DOM branch must anchor itself to a visible component so that it can style and customize it.
The unique aspect about a shadow DOM branch is its encapsulation. Everything used to style a shadow DOM branch’s root element is private to it. No other element can access it.
Angular embraces this form of encapsulation with components. The style-sheet and template of a component encapsulate together. No other components have access to them. Style-sheet clashes cannot occur.
Angular does not use the shadow DOM by default. It uses an emulation system that mimics the behavior of the shadow DOM. This is a temporary measure since some web browsers do not yet support the shadow DOM API.
The @Component
metadata contains the encapsulation
field. This lets developers toggle in-between emulated shadow DOM, real shadow DOM, or neither. Here are the options in their respective order:
ViewEncapsulation.Emulated
- fake shadow DOM (default)ViewEncapsulation.Native
- real shadow DOM (now deprecated since Angular 6.0.8)ViewEncapsulation.None
- neither
ViewEncapsulation.None
means the component’s style-sheets elevate to the global scope. Not recommended considering components should form their own private unit (encapsulation). Angular still provides it as an escape hatch for extreme situations.
Conclusion
Components build applications. They are privately scoped and separately uniform of each other unless configured otherwise. Applications tend to begin from the root module. Past that, components form an elongated tree defining the rest of the application.
A component covers a designated unit of the application interface. This includes its styles, logic, and layout. Other schematics such as pipes, services, and directives see frequent use in component code. You can learn more about these interactions in some of the other Angular guide articles.
Do not forget that components must bootstrap. This can happen in the root module or the component’s metadata. This is so Angular recognizes the component wherever it appears in the application.
You can always learn more, as components carry far more depth than that what this article could convey.