0

How can i extract specific data from the array with the id and use it in the div.

home.component.ts file

import { Component } from '@angular/core';
import { HomeProjectComponent } from './home-project.component';
import { NewsletterComponent } from './../shared/newsletter/newsletter.component';

export class Project {
  id: number;
  name: string;
  title: string;
  status: string;
}

const PROJECTS: Project[] = [
  { id: 1,
    name: 'name1',
    title: 'title1',
    status: 'status1'
  },
  { id: 2,
    name: 'name2',
    title: 'title2',
    status: 'status2'
  },
  { id: 3,
    name: 'name3',
    title: 'title3',
    status: 'status3'
  },
....
{ id: 100,
    name: 'name100',
    title: 'title100',
    status: 'status100'
  },
];


@Component({
  selector: 'home',
  templateUrl: './app/home/home.component.html',
  styleUrls: ['./app/home/home.component.css'],
  directives: [HomeProjectComponent, NewsletterComponent]
})

export class HomeComponent {
  projects = PROJECTS;
}

This is my html code were i m trying to get the name, title and status of the project with ID home.component.html

The {{project.name}}, {{project.title}} and {{project.status}} must be the same

get data with id 20
<home>
  <h1>{{project.name}}</h1>
  <h2>{{project.title}}</h2>
  <p>{{project.status}}</p>
</home>

// SOME HTML

get data with id 64
<home>
  <h1>{{project.name}}</h1>
  <h2>{{project.title}}</h2>
  <p>{{project.status}}</p>
</home>

// SOME HTML

get data with id 69
<home>
  <h1>{{project.name}}</h1>
  <h2>{{project.title}}</h2>
  <p>{{project.status}}</p>
</home>
4
  • How does the component code look like? Commented Jun 17, 2016 at 12:15
  • @Component({ selector: 'home', templateUrl: './app/home/home.component.html', styleUrls: ['./app/home/home.component.css'], directives: [HomeProjectComponent, NewsletterComponent] }) Commented Jun 17, 2016 at 12:20
  • And the component class? Please edit your question and add the code there. Code in comments is almost unreadable. Commented Jun 17, 2016 at 12:21
  • ok. just update my code. Commented Jun 17, 2016 at 12:29

2 Answers 2

1
@Component({
  selector: 'home',
  templateUrl: './app/home/home.component.html',
  styleUrls: ['./app/home/home.component.css'],
  directives: [HomeProjectComponent, NewsletterComponent]
})

export class HomeComponent {
  projects = PROJECTS;

  projectId2 = this.extractProjectWithId(2);

  extractProjectWithId(id){
      let i;
      for(i = 0; i< this.projects.length; i++)
      {
         if(this.projects[i].id === id)
          return this.projects[i];
      }

      return null;
    }
}

<div *ngIf="projectId2"> // extract data with ID 2 from project 
  <h1>{{projectId2.name}}</h1>
  <h2>{{projectId2.title}}</h2>
  <p>{{projectId2.status}}</p>
</div>
Sign up to request clarification or add additional context in comments.

4 Comments

This way i can only extract the data with id2. And i want 3* same html element but extract others id
I just update my question so you can understand what i mean
you can just use *ngFor then
ngFor display all objects in order. i'd like to use a home-id-x then some html codes then home-id-xx some html code and home-id-xxx.
1

I would write a method returning object with id 2. Something like:

getObjectById(id: number) {
  return PROJECTS.filter(x => x.id == id);
}

Reading it

<home> // extract data with ID 2 from project 
  <h1>{{ getObjectById(2)[0].name }}</h1>
  <h2>{{ getObjectById(2)[0].title }}</h2>
  <p>{{ getObjectById(2)[0].status }}</p>
</home>

Update for new edit:

Since you need all project from array why don't you print them using *ngFor:

<home *ngFor="let project of PROJECTS"> // extract data with ID 2 from project 
  <h1>{{ project.name }}</h1>
  <h2>{{ project.title }}</h2>
  <p>{{ project.status }}</p>
</home>

That simplifies interpolations {{ }} and you won't need any other functions to get specific element. If you care about the sequence of projects, then create a member variable

projectInSequence: Project[];

and push projects in desired sequence, ngFor will change to *ngFor="let project of projectInSequence. Filling projectInSequence can be done in a life-cycle hook https://angular.io/docs/ts/latest/guide/lifecycle-hooks.html

2 Comments

Well, i don't need/want to display all the projects. I have over 20 projects and i wan t to display only 9 of them, i want to have one project id-x then some html than project-id-y some html project-id-zz some html and so on
Then better go with projectsInSequence way

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.