rangle / angular-training-examples Goto Github PK
View Code? Open in Web Editor NEWBlog application for Angular 2 training
Blog application for Angular 2 training
Repo structure for holding all knowledge components, while allowing for static serving and automatic rebuilds.
Keep all examples isolated and self-contained.
From my perspective, when doing training we should try to keep our examples as simple as possible to not distract the students from what's really important to learn in every section.
For example, in the example of this section we have:
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
template: `<input
type="button"
value="{{ label }}"
(click)="onClick()">
<span>{{ randomString }}</span>`,
styles: [``]
})
export class AppComponent {
label = 'randomize';
randomString = randomString();
onClick() {
this.randomString = randomString();
};
}
function randomString() {
const length = (Math.floor(Math.random() * 25) + 10);
let str = '';
for (let i = 0; i < length; i += 1) {
str += String.fromCharCode(Math.floor(Math.random() * 255))
}
return str;
}
Some people might be distracted by trying to understand how the function randomString
works when the goal of this chapter is to focus on how the event system works in Angular. A simple counter might be enough to showcase events and keep people focused on what's really important. Also, calling a function from outside the class relying on hoisting is adding an additional layer of complexity from people coming from traditional OOP languages that are used to rely on methods.
I can submit a PR but I wanted to know first if you agree with me on this.
I have detected some inconsistencies between the code examples and the official style guide. Here are my findings and suggestions:
ChildComponent
and the name of the file being child.ts
. In that case the filename should be child.component.ts
.example.module.ts
we should follow the convention an name it app.module.ts
.index.html
we should follow the convention and name it app.component.ts
.Component
so instead of naming a component like:component-1.ts
@Component({
selector: 'component-1',
template: 'Component 1',
})
export class Component1 {}
Do:
first.component.ts
@Component({
selector: 'rio-first',
template: 'First Component',
})
export class FirstComponent {}
import { AppComponent, ChildComponent } from './';
do: import { AppComponent } from './app.component';
import { ChildComponent } from './child.component';
rio-
. So instead of having app-root
use rio-app
.There's not an official recommendation about this, but every example that I see in angular.io declare the properties of the NgModule
decorator in this order:
@NgModule({
imports: [],
providers: [],
declarations: [],
exports: [],
bootstrap: []
})
This is just a recommendation but having the imports
at the top and exports
at the bottom (for feature modules) is similar to the order we have in a normal ES6 module. Also having declarations
close to exports
makes it easy to spot which elements are public or private to the module.
The Angular team is now recommending of creating a separate modules for routes. So instead of having a file like this:
routes.ts
import { Routes } from '@angular/router';
import { Component1 } from './component-1';
import { Component2 } from './component-2';
export const routes: Routes = [
{ path: '', redirectTo: 'component-1', pathMatch: 'full' },
{ path: 'component-1', component: Component1 },
{ path: 'component-2', component: Component2 },
{ path: '**', component: Component1 },
];
Do:
app-routing.module.ts
import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router';
import { FirstComponent } from './first.component';
import { SecondComponent } from './second.component';
@NgModule({
imports: [
RouterModule.forRoot([
{ path: '', redirectTo: 'first', pathMatch: 'full' },
{ path: 'first', component: FirstComponent },
{ path: 'second', component: SecondComponent },
{ path: '**', component: FirstComponent },
])
],
exports: [
RouterModule,
],
})
export class AppRoutingModule {}
And then in your root module:
...
@NgModule({
imports: [
BrowserModule,
AppRoutingModule,
],
...
})
export class AppModule {}
In the code example for the section 4.1-what-if
we have this code example:
@Component({
selector: 'app-root',
template: `<input
type="button"
value="{{ label }}"
(click)="onClick()">
<span *ngIf="label !='show'">{{ someString }}</span>`,
styles: [``]
})
export class AppComponent {
label = 'show';
someString = 'What if you could conditionally show components?';
onClick() {
if (this.label === 'show') {
this.label = 'hide';
} else {
this.label = 'show';
}
};
}
In my opinion is not a good practice to have logic in the template like *ngIf="label !='show'"
, instead we should rely on boolean instances like *ngIf="isVisible"
and delegate the logic to the class. Again, I can submit a PR if you agree.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.