angulartypescriptangular-componentsangular-component-life-cycle

Angular 6 - @input is not working


I have written a simple angular application to learn @input to communicate between components but the value is not being passed. I have also read some forums who have faced similar problems but none of the solutions prescribed are working for me. Please guide.

app.componenent.html

<app-task [prioirty]="'p1'"></app-task>

task.component.ts

import { Component, OnInit, Input } from '@angular/core';
import { Task } from 'src/app/task/task';
import { TaskService } from 'src/app/task/services/task.service';
import {AppComponent} from 'src/app/app.component'

@Component({
  selector: 'app-task',
  templateUrl: './task.component.html',
  styleUrls: ['./task.component.css'],
})
export class TaskComponent implements OnInit {     
  constructor(private taskService: TaskService) {        
  }      
  task: Task = new Task();
  @Input() priortiy: string; 
  ngOnInit() {
  }
  addTask(){
    alert(this.priortiy);
    this.taskService.addTask(this.task).subscribe((data : Task) => {}, error => console.error(error),() => console.log("Job Added successfully"));    
  }    
}

When I am trying to display the value of priority in an alert box, I get the value as undefined.

Let me know if anyone wants me to place the complete code.


Solution

  • You have a typo,

    Change from

     @Input() priortiy: string; 
    

    To

     @Input() prioirty: string; 
    

    and alert too,

    alert(this.prioirty);