With AngularJS Bucharest
In the community:
In the tech world:
Problems with JS
class Pizza {
constructor(toppings) {
this.toppings = toppings
}
listToppings() {
toppings.forEach(function(topping) {
console.log(topping.toString();
}
}
}
class Pizza {
constructor(toppings) {
this.toppings = toppings
}
listToppings() {
this.toppings.forEach(function(topping) {
console.log(topping.toString();
}
}
}
var pizza = new Pizza();
pizza.listToppings();
var toppingPrice = toppings.map(function(topping) {
return topping.name + " " + topping.price;
});
var toppingPrice = toppings.map(topping => topping.name +
" " + topping.price );
console.log('hello my name is ' + name + ' I am ' + age );
console.log(`hello my name is ${name}, and I am ${age}`);
var element = `
<div>
<span>Some text</span>
</div>
`;
const a = 123;
a = 124; // Error
...
if(someCondition) {
let someVar = 10;
...
}
someVar = 2 // not same as above
for(let i=0; i<10;i ++) {
... -> life of i
}
let cde = ['c', 'd', 'e'];
let scale = ['a', 'b', ...cde, 'f', 'g'];
function add(...numbers) {
return numbers[0] + numbers[1];
}
let foo = ['one', 'two', 'three'];
let [one, two, three] = foo;
let someObj = {a: "asd", b: "qwe"};
let {a, b} = someObj;
What TypeScript brings extra:
let isDone: boolean = false;
let height: number = 6;
let name: string = "bob";
let list: number[] = [1, 2, 3];
let list: Array<number> = [1, 2, 3];
enum Color {Red, Green, Blue};
let c: Color = Color.Green;
let notSure: any = 4;
notSure = "maybe a string instead";
notSure = false; // okay, definitely a boolean
class Person {
name: string;
nickName?: string;
constructor(name: string) {
...
}
}
const p = new Person("Andrei");
interface ClockInterface {
currentTime: Date;
setTime(d: Date);
}
class Clock implements ClockInterface {
currentTime: Date;
setTime(d: Date) {
this.currentTime = d;
}
constructor(h: number, m: number) { }
}
//declare
private _possessions: Array<Thing>;
constructor(){
//assign
this._possessions = new Array<Thing>();
}
Functions that are invoked with a prefixed @ symbol, and immediately followed by a class , parameter, method or property.
@Component({
selector: 'my-app',
styles: [`
h1 {color:#545454;}
`]
template: `
<h1>Hello from the {{componentName}}.</h1>
`
})
export class AppComponent {
componentName: 'AppComponent'
}
Angular libraries:
Polyfils and extra libraries:
import { Component } from '@angular/core';
import { SomeService } from './services/SomeService';
@Component({
selector: 'my-component',
template: `
<div>Hello my name is {{name}}.
<button (click)="sayMyName()">
Say my name
</button>
</div>`
})
export class MyComponent {
@Input() name
constructor(private someService: SomeService) {
}
sayMyName() {
console.log('My name is', this.name)
}
}
app.module('someModule')
.component('my-component', {
bindings: {
name: '<',
sayMyName: '&'
},
template: '<div>Hello my name is {{name}}. <button (click)="sayMyName()">Say my name</button></div>',
controller: ComponentController
})
class ComponentController {
constructor(SomeService) {
this.someService = SomeService;
}
sayMyName() {
console.log('My name is', this.name)
}
}