Bienvenido a nuestro espacio, ahora hallarás la solucíon a lo que buscabas.
Ejemplo 1: angular @Output ()
/* @Output()
- allows data to flow from the child class to the parent class
- normally initialized with EventEmitter
*/// Parent ComponentimportComponentfrom'@angular/core'
@Component(
selector: app-component,
template:`
- item
`/* Template Notes
- (newItemEvent)='addItem($event)' -- the event binding
tells Angular to connect the event in the child
component, newEventItem, to the method in the parent,
addItem(), and that the event that the child is
notifying the parent of is to be the argument of the
method.
- $event contains the data that the child component is
passing to the parent.
*/)exportclassAppComponent
items =['item1','item2','item3'];addItem(newItem: string)this.items.push(newItem);/* Class Notes
- `[item]` -- @Input() target from child
- `currentItem` -- property from parent
- `(deleteRequest)` -- the target @Output event from
child
- `crossOffItem($event)` -- method from parent;
*/// Child ComponentimportOutput,EventEmitterfrom'@angular/core';
@Component(
selector: app-item-output,
template:`
`/* Template Notes
- #newItem stores whatever is typed into the input
- (click) event is bound to addNewItem method and
passes newItem.value to the component class
*/)exportclassItemOutputComponent
@Output() newItemEvent =newEventEmitter<string>();addNewItem(value: string)// this method emits the value of newItemEventthis.newItemEvent.emit(value);/* Component Class Notes
- @Output marks the property as a way for data to go
from the child component to the parent component
- newItemEvent is the name of the @Output
- new EventEmitter() -- tells Angular to create
a new event emitter of type string
- addNewItem(value: string) -- when the user clicks the
button the child component lets the parent component
know about the event and gives the data to the parent.
*/
Ejemplo 2: enviar datos angulares al componente principal
importComponent,Output,EventEmitterfrom'@angular/core';
@Component(
selector:'app-child',
template:`
`,
styleUrls:['./child.component.css'])exportclassChildComponent
message: string ="Hola Mundo!"
@Output() messageEvent =newEventEmitter<string>();constructor()sendMessage()this.messageEvent.emit(this.message)
Ejemplo 3: compartir datos entre componentes angulares
importComponent,Inputfrom'@angular/core';//Child-Component
@Component(
selector:'app-child',
template:`
Say message
`,
styleUrls:['./child.component.css'])exportclassChildComponent
@Input() childMessage: string;constructor()======================================================================importComponentfrom'@angular/core';//Parent-Component data transfer to Child-Component
@Component(
selector:'app-parent',
template:`
`,
styleUrls:['./parent.component.css'])exportclassParentComponent
parentMessage ="message from parent"constructor()
Ejemplo 4: compartir datos usando el servicio angular 6
importInjectablefrom'@angular/core';importBehaviorSubjectfrom'rxjs';
@Injectable()exportclassDataServiceprivate messageSource =newBehaviorSubject('default message');
currentMessage =this.messageSource.asObservable();constructor()changeMessage(message: string)this.messageSource.next(message)
Si posees alguna desconfianza y forma de modernizar nuestro enunciado eres capaz de dejar una observación y con deseo lo ojearemos.
¡Haz clic para puntuar esta entrada!
(Votos: 2 Promedio: 4)