Q:

input property angular

/* @Output()
- allows data to flow from the child class to the parent class
- normally initialized with EventEmitter
*/ 

// Parent Component
import { Component } from '@angular/core'
@Component({
  selector: app-component,
  template: 
  `
  <app-item-output (newItemEvent)='addItem($event)'> </app-item-output>
  <ul>
      <li *ngFor='let item of items'>{{item}}</li>
  </ul>
  `
  /* 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.
  */
 }) 
 export class AppComponent {
 	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 Component
import { Output, EventEmitter } from '@angular/core';
        
@Component({
    selector: app-item-output,
    template: 
    `
        <label>Add an item: <input #newItem></label>
        <button (click)='addNewItem(newItem.value)'>Add to parent's list</button>
    `
  	/* 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
    */
})

export class ItemOutputComponent {
    @Output() newItemEvent = new EventEmitter<string>();
	
    addNewItem(value: string) {
        // this method emits the value of newItemEvent
        this.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<string>() -- 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.
    */
}

5
/* @Input()
- @Input is used in the child component to indicate that the 
  component can receive its value from the parent component
*/

// Parent component
@Component({
    template: `<app-item-detail [item]='currentItem'></app-item-detail>`
})
                
export class AppComponent {
    currentItem = 'Television';
}

/* Parent Component Notes
- `<app-item-detail [item]='currentItem'></app-item-detail>`
- `<app-item-detail><app-item-detail> is the child selector
- [item] is the target. It has to be the same as the @Input in the child class
- currentItem is the source property from the parent
*/ 

// Child Component
import { Component, Input } from '@angular/core';

@Component({
    template: `
                <p>Today's Item: {{ item }}
              `
})

export class ItemDetailComponent {
    @Input() item: string; //decorate the property with @Input()
    @Input('alias name') item: string;
    // the type of the @input can be of any type
}

/* Child Component Notes
- `@Input item: string` indicates that the child component
   will take the value from the parent component
*/
3
//passing properties to child elements
<app-slider [title]="'This is a title'"> </app-slider>

//inside the component slider i add just before the constructor :
.
.
@input() title: string;
constructor(){
}


//after this we can use these new property inside the .html file of the component
<div id='slider' class='slider-big'>
  <h1> {{title}}</h1>

</div>

0

New to Communities?

Join the community