0

I am working on Angular and I populate a div with the contents of an array of Objects. I want to pass the object I click on to be passed as a reference to the router link which takes id as input:

<a [routerLink]="['/edit-emp-reactive',employee.id]">Edit</a> This links to an employee editing template that takes the id provided for editing details of the employee.

How do I pass the object as a reference This is the component HTML:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>

    <b>Employees List</b>
    <br>
    <br>

            <div class="form-group">
                <label for="search">Search</label>
                <input type="text" (keyup)="search()" id="search" [ngModel]="searchText"  (ngModelChange)="searchKey($event)"  placeholder="Find by name" ngModel>
            </div>

    <div class="row" *ngFor="let e of filteredEmployees">
        <div class="block" style="width:300px;height:100px;border:1px solid #000;">
            <h4>{{e.name}}</h4>
            <h6>₹ {{e.salary}}</h6>
            <app-employee-info></app-employee-info>
        </div>
        <br>
        <br>
    </div>
    
</body>
</html>

This is the component file:

import { Component, OnInit } from '@angular/core';
import { Employee } from '../Employee';
import {NgModule} from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';


@Component({
  selector: 'app-employee-list',
  templateUrl: './employee-list.component.html',
  styleUrls: ['./employee-list.component.css']
})
export class EmployeeListComponent implements OnInit {
  
  constructor() { }
  

  ngOnInit(): void {
    this.filteredEmployees=this.employeeList
    
  }
  
  

  filteredEmployees:Employee[]=[]

  searchText=""

  employeeList:Employee[]=
  [
    {
    id:1,
    name:"abc def",
    salary:20000,
    permanent:true,
    department:{id:1, name:"Payroll"},
    skill:[{id:1 , value:"HTML"},{id:2 , value:"CSS"},{id:1 , value:"JS"}],
    dateOfBirth:new Date('01/03/2002')
    },
    {
    id:1,
    name:"ssss gggg",
    salary:40000,
    permanent:false,
    department:{id:2, name:"Internal"},
    skill:[{id:1 , value:"HTML"},{id:2 , value:"CSS"},{id:1 , value:"JS"}],
    dateOfBirth:new Date('21/03/2006')
    },
    {
    id:1,
    name:"asdf zxcv",
    salary:60000,
    permanent:true,
    department:{id:3, name:"HR"},
    skill:[{id:1 , value:"HTML"},{id:2 , value:"CSS"},{id:1 , value:"JS"}],
    dateOfBirth:new Date('16/05/2010')
    },
    {
      id:1,
      name:"ghji",
      salary:90000,
      permanent:true,
      department:{id:3, name:"HR"},
      skill:[{id:1 , value:"HTML"},{id:2 , value:"CSS"},{id:1 , value:"JS"}],
      dateOfBirth:new Date('1/03/2003')
      }
  ];

  cEmployees = this.employeeList;

  searchKey(data:string)
  {
    this.searchText = data;
    this.search();
  }

  search()
  {
    
    this.filteredEmployees = (this.searchText === ""|| this.searchText===null)? this.employeeList : this.employeeList.filter((element) => {
      return element.name.toLowerCase() == this.searchText.toLowerCase();
    });
  }

}
2
  • Where is the link? Inside <app-employee-info></app-employee-info>? Commented Apr 30, 2021 at 6:54
  • @Chrillewoodz yes Commented Apr 30, 2021 at 6:56

2 Answers 2

1

Simply add this code:

<app-employee-info [employee]="e"></app-employee-info>

And inside your info component add:

@Input() employee: <your-employee-type>;
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot . Really appreciate it.I am new to Angular and was trying to find the syntax to pass the object but strangely couldn't find it..
1

In app-employee-info component you should decorate a variable with the @input decorator, that way you will be able to pass input parameter to the component, which you can reach in the component class and the HTML template too. Check the documentation here: https://angular.io/api/core/Input

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.