Created
October 4, 2019 13:57
-
-
Save rpf1980/83f3d3d369efe3cbc250a75b624eb171 to your computer and use it in GitHub Desktop.
ANGULAR: Comunicación básica entre componentes (Input)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//------------------ CODE: component.ts ------------------------ | |
import { Component, OnInit } from '@angular/core'; | |
//MODELO DE DOMINIO de nuestra clase JokePoolComponent | |
class Joke{ | |
//Atributos | |
jkTitle: string; | |
jkText: string; | |
hide: boolean; | |
constructor(jkTitle:string, jkText:string){ | |
this.jkTitle = jkTitle; | |
this.jkText = jkText; | |
this.hide = true; | |
} | |
toggle(){ | |
this.hide = !this.hide; | |
} | |
} | |
@Component({ | |
selector: 'app-jokes-pool', | |
templateUrl: './jokes-pool.component.html', | |
styleUrls: ['./jokes-pool.component.css'] | |
}) | |
export class JokesPoolComponent implements OnInit { | |
//Atributos | |
jokes: Object[]; //Instanciamos la clase usada para el modelo de dominio | |
constructor() { | |
this.jokes = [ | |
new Joke("¿Por qué las focas del circo miran siempre hacia arriba?", | |
"Porque es donde están los focos."), | |
new Joke("¿Por qué estás hablando con esas zapatillas?", | |
"Porque pone converse."), | |
new Joke("¿Sabes cómo se queda un mago después de comer?", | |
"Magordito."), | |
new Joke("Me da un café con leche corto.", | |
"Se me ha roto la máquina, cambio."), | |
new Joke("Buenos días, me gustaría alquilar 'Batman Forever'.", | |
"No es posible, tiene que devolverla tomorrow."), | |
new Joke("Abuelo, ¿por qué estás delante del ordenador con los ojos cerrados?", | |
"Es que Windows me ha dicho que cierre las pestañas."), | |
new Joke(" ¿Qué es un pez en un cine?", | |
"Un mero espectador."), | |
]; | |
} | |
ngOnInit() { | |
} | |
} | |
//------------------ CODE: component.html ------------------------ | |
<div class="d-flex align-content-start flex-wrap" *ngFor = "let item of jokes"> | |
<h4>{{item.jkTitle}}</h4> | |
<p [hidden]="item.hide">{{item.jkText}}</p> | |
<button class="btn btn-outline-secondary btn-sm" type="button" (click)="item.toggle()">Click here</button> | |
</div> | |
//------------------ CODE: vista del módulo ------------------------ | |
<h1 class="display-4">CHISTES RÁPIDOS</h1><br> | |
<app-jokes-pool></app-jokes-pool> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment