|
| 1 | +# Conceptos Angular |
| 2 | + |
| 3 | +## Interpolation |
| 4 | + |
| 5 | +```ts |
| 6 | +@Component({...}) |
| 7 | +class MyComponent { |
| 8 | + publicProperty = "Cualquier valor"; |
| 9 | +} |
| 10 | +``` |
| 11 | + |
| 12 | +```html |
| 13 | +<p>{{ publicProperty }}</p> |
| 14 | +``` |
| 15 | + |
| 16 | +## Bindings |
| 17 | + |
| 18 | +- Attribute Binding `[]` |
| 19 | + - Primitives: strings, number, obj, date... NO Funcs |
| 20 | +- Event Binding `()` |
| 21 | + - Feed with `callback`: Class Component method |
| 22 | + |
| 23 | +```ts |
| 24 | +class MyComponent { |
| 25 | + publicProperty = "Cualquier valor"; |
| 26 | + |
| 27 | + myValue = "un valor..."; |
| 28 | + |
| 29 | + inputEvent($event: any) { |
| 30 | + this.myValue = $event.target.value; |
| 31 | + } |
| 32 | +} |
| 33 | +``` |
| 34 | + |
| 35 | +```html |
| 36 | +<p>{{ publicProperty }}</p> |
| 37 | + |
| 38 | +<input [value]="myValue" (input)="inputEvent($event)" /> |
| 39 | +``` |
| 40 | + |
| 41 | +## Custom Events |
| 42 | + |
| 43 | +- `@Output` |
| 44 | + - para registrar el atributo de evento para poder pasar un `callback` |
| 45 | +- `EventEmitter` |
| 46 | + - Permite lanzar el evento |
| 47 | + |
| 48 | +## Directiva estructural |
| 49 | + |
| 50 | +- `NgFor` |
| 51 | + |
| 52 | +```ts |
| 53 | +class Component { |
| 54 | + fruits = ["banana", "apple", "melon"]; |
| 55 | +} |
| 56 | +``` |
| 57 | + |
| 58 | +```html |
| 59 | + |
| 60 | +<tr *ngFor="let fruit of fruits">{{fruit}}</tr> |
| 61 | +<tr>banana</tr> |
| 62 | +<tr>apple</tr> |
| 63 | +<tr>melon</tr> |
| 64 | +``` |
| 65 | + |
| 66 | +## Formularios |
| 67 | + |
| 68 | +- Template - Motor de validaciones HTML5 |
| 69 | + - required, min, max... |
| 70 | +- Reactive - Motor de validaciones provisto por 'Angular' |
| 71 | + - Permite validaciones pesonalizadas |
| 72 | + - Soporte a validaciones asíncronas |
| 73 | + |
| 74 | +- `ngModel` |
| 75 | + - Acumula validaciones a nivel de campo de formulario |
| 76 | + - Two way binding |
| 77 | +- `ngForm` |
| 78 | + |
| 79 | +## Dependency Injection |
| 80 | + |
| 81 | +```ts |
| 82 | +class Employee { |
| 83 | + constructor(private salaryReport: ISalaryReport) { |
| 84 | + // salaryReport = new SalaryReport(); |
| 85 | + } |
| 86 | + |
| 87 | + calcSalary() { |
| 88 | + this.salaryReport.....() |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +interface ISalaryReport { |
| 93 | + getSalaryByHour(hours: number) {} |
| 94 | + |
| 95 | + getSalaryByRole(role: string) {} |
| 96 | +} |
| 97 | + |
| 98 | +class A implements ISalaryReport {} |
| 99 | +class B implements ISalaryReport {} |
| 100 | + |
| 101 | +const e = new Employee(new A()) |
| 102 | +const e = new Employee(new B()) |
| 103 | +``` |
| 104 | + |
| 105 | +## Routing |
| 106 | + |
| 107 | +- Definimos diccionario de rutas para que el servicio de `Router` las pueda usar |
| 108 | +- `router-outlet` directiva donde el router inyectara nuestros componentes |
| 109 | +- `routerLink` |
| 110 | +- `routerLinkActive` |
| 111 | + |
| 112 | +## Observables |
| 113 | + |
| 114 | +### Terminos y Sintaxis |
| 115 | + |
| 116 | +- **Observable:** Cualquier flujo (`stream`) de datos |
| 117 | +- **Observer:** Observa el flujo de datos. Métodos para procesar 'notificaciones' del flujo de datos (`stream`): `next()`, `error()`, `complete()` |
| 118 | +- **Subscriber:** Un observador (`Observer`) que se puede de-subscribir del flujo de datos (`unsubscribe`) |
| 119 | +- **Subscription:** Representa la ejecución de un Observable. El método `subscribe()` devuelve una 'Subscripción' |
| 120 | + |
| 121 | +### Creando Observables |
| 122 | + |
| 123 | +Podemos crear un Observable mediante: |
| 124 | + |
| 125 | +- Constructor |
| 126 | +- Funciones de creación: |
| 127 | + - of, from, fromEvent, interval, ... |
| 128 | +- Angular Features |
| 129 | + - Forms: valueChanges |
| 130 | + - Routing: paramMap |
| 131 | + - HttpClient: get, post, ... |
| 132 | + |
| 133 | +### Arrancando Observables |
| 134 | + |
| 135 | +Un Observable on enitira hasta que se 'arranca': |
| 136 | + |
| 137 | +- Llamar a `subscribe` |
| 138 | +- Alimentando un `Observer` |
| 139 | + - `next()`, `error()`, `complete()` |
| 140 | + |
| 141 | +### Parando Observables |
| 142 | + |
| 143 | +Para parar un Observable: |
| 144 | + |
| 145 | +- Llamar a `complete()` en el `Observer` |
| 146 | +- Utilizar una función de creación que termine |
| 147 | + - of, from, ... |
| 148 | +- Utilizar un operador que termine |
| 149 | + - take, takeUntil, ... |
| 150 | +- Emitir un error |
| 151 | +- Llamar `unsubscribe()` en el objecto Subscripción |
| 152 | + |
| 153 | +### Operadores |
| 154 | + |
| 155 | +#### `map` |
| 156 | + |
| 157 | +``` |
| 158 | +------------[55]-----------------[80]----|-> |
| 159 | +
|
| 160 | +map((x) => x % 2) |
| 161 | +
|
| 162 | +------------[1]------------------[0]----|-> |
| 163 | +``` |
| 164 | + |
| 165 | +#### `combineLatest` |
| 166 | + |
| 167 | +``` |
| 168 | +---[45]-[50]-----------------------------|-> |
| 169 | +
|
| 170 | +------------[55]-----------------[80]----|-> |
| 171 | +
|
| 172 | +--[25]------------------[75]-------------|-> |
| 173 | +
|
| 174 | +combineLatest([obv1, obv2, obv3]) |
| 175 | +
|
| 176 | +--[50,55,25]-[50,55,75]-[50,80,75]-------|-> |
| 177 | +``` |
| 178 | + |
| 179 | +#### `forkJoin` |
| 180 | + |
| 181 | +``` |
| 182 | +---mist-silver-----------------pearl|------> |
| 183 | +
|
| 184 | +------------blue-gray|---------------------> |
| 185 | +
|
| 186 | +--¡------------------taupe|----------------> |
| 187 | +
|
| 188 | +forkJoin([obv1, obv2, obv3]) |
| 189 | +
|
| 190 | +------------------------[pearl,gray,taupe]|> |
| 191 | +``` |
| 192 | + |
| 193 | +#### `withLatestFrom` |
| 194 | + |
| 195 | +``` |
| 196 | +---A1-------A2-----------------A3-----|-> |
| 197 | +
|
| 198 | +-------S1--------------------------S2-|-> |
| 199 | +
|
| 200 | +--C1------------------C2--------------|-> |
| 201 | +
|
| 202 | +apple$.pipe(withLatestFrom(stick$, caramel$)) |
| 203 | +
|
| 204 | +-----------[A2,S1,C1]----[A3,S1,C2]---|-> |
| 205 | +``` |
| 206 | + |
| 207 | +#### `filter` |
| 208 | + |
| 209 | +``` |
| 210 | +---A-------B-----------------A-----|-> |
| 211 | +
|
| 212 | +filter(item => item === 'A') |
| 213 | +
|
| 214 | +---A-------------------------A-----|-> |
| 215 | +``` |
| 216 | + |
| 217 | +#### `startWith` |
| 218 | + |
| 219 | +``` |
| 220 | +---A-------B-----------------A-----|-> |
| 221 | +
|
| 222 | +startWith('Orange') |
| 223 | +
|
| 224 | +---OA-------B-----------------A-----|-> |
| 225 | +``` |
| 226 | + |
| 227 | +#### `merge` |
| 228 | + |
| 229 | +``` |
| 230 | +---A-------B-----------------A-----|-> |
| 231 | +-------O-------------G-------------|-> |
| 232 | +
|
| 233 | +merge(...) |
| 234 | +
|
| 235 | +---A---O---B---------G-------A-----|-> |
| 236 | +``` |
| 237 | + |
| 238 | +#### `scan` |
| 239 | + |
| 240 | +``` |
| 241 | +---2-------5-----------------9-----|-> |
| 242 | +
|
| 243 | +scan((acc, curr) => acc + curr) |
| 244 | +
|
| 245 | +---2-------7-----------------16----|-> |
| 246 | +``` |
| 247 | + |
| 248 | +### Observables Manejo errores |
| 249 | + |
| 250 | +Dos estrategias a la hora de tratar errores: |
| 251 | + |
| 252 | +- catch and rethrow |
| 253 | +- catch and replace |
| 254 | + |
| 255 | +## Signals |
| 256 | + |
0 commit comments