How can I implement a master-detail view in Angular 2?

Update: I finally figured it out. After moving all the html from index.html to app.component.ts, everything started working perfectly. The method described below is the correct one.

I've been facing an issue where I have a ParentComponent that retrieves data from a service and displays it in a sidebar. When clicking on an item in the sidebar, it should display details about that parent component in the main section of the page. Since this requires using two different templates, my solution was to establish a parent-child relationship with the sidebar as the parent and the detail section as the child, passing the necessary data to the child for display. Does this approach seem appropriate? Is there a better way to tackle this problem? I've experimented with various methods, but they all seem outdated as they rely on directives which are no longer supported.

Update: This question differs from the other one because the answer mentions directives which were phased out in the rc6 release of angular2. This question pertains to post-rc6 changes.

Additional Code snippet:

index.html:

<header>
  <div class="content">This is a header</div>
</header>
<div class="container">
    <div class="sidebar">
      <div class="content">
        <parent-items></parent-items>
      </div>
    </div>
    <div class="main-content">
      <div class="content">
        <my-app>Loading...</my-app>
        <child-cmp [id]="selectedItem.id"></child-cmp>
      </div>
    </div>
    <div class="clearfix"></div>
</div>

child-cmp.ts:

@Component({
  selector: 'child-cmp',
})
export class ChildComponent {
  @Input() set id(n) {
    this.getData(n)
  }
  getData(id: number): void {
     console.log('triggered');
  };
}

parent.ts:

@Component({
  moduleId: module.id,
  selector: 'parent-items',
  templateUrl: `<ul class="items">
      <li *ngFor="let item of items"
          (click)="selectedItem = item"
          [class.selected]="item === selectedItem">{{item.name}}</li>
      </ul>`,
})
export class ItemsComponent implements OnInit {
  items: Item[];
  selectedItem: Item;
  constructor(private itemService: ItemService) {};
  getItems(): void {
    this.itemService
        .getItems()
        .then(items => this.items = items);
  }
  ngOnInit(): void {
    this.getItems();
  }
}

Answer №1

Seems like a valid approach. Let's consider a scenario where the parent component contains an array of identifiers called children.

Parent Template (Sidebar)

<div *ngFor="let child of children">
    <a (click)="currentChild = child">{{child.name}}</a>
</div>

When a link is clicked, it updates the current child. The main section will then display the details of the current child.

Parent Template (Main)

<child-cmp [id]="currentChild.id"></child-cmp>

The child component needs to use @Input to receive a new child identifier. This allows for dynamically updating the child component with new data based on the identifier.

Child Component

/** Fetches data from the server and assigns it to local variables */
getData(id){
    this.http.get('/api/children/'+id).map(data => data.json()).subscribe(
        data => { this.name = data.name; }
    );
}

/** Executes whenever a new id is received */
@Input() set id(n){ this.getData(n) }

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Using conditional statements to localize in Angular

In my Angular application, I am utilizing i18n for internationalization purposes. When it comes to HTML, I use i18n="@@<unique_Id>" and for dynamic elements defined in TypeScript class files, I use $localize. For example: this.buttontext = ...

Using static methods within a static class to achieve method overloading in Typescript

I have a TypeScript static class that converts key-value pairs to strings. The values can be boolean, number, or string, but I want them all to end up as strings with specific implementations. [{ key: "key1", value: false }, { key: "key2&qu ...

Utilizing TypeScript generic types as a key for an object

function createRecord<T extends string>(key: T): Record<T, string> { return { [key]: 'asdf' }; } Encountering an issue: The type '{ [x: string]: string; }' is not matching with the expected 'Record<T, st ...

The base component is not updating the property from the inherited component

Working on an Angular project where I have a component that inherits from a base component. @Component({ selector: "my-baseclass-component", template: ` <div style="border:1px solid red;padding:10px"> Counter Value (Check Co ...

Leveraging Angular 4 for efficient integration and application of d3 and d3-cloud libraries along with their corresponding typings

My goal is to create a word cloud using d3 + d3-cloud in Angular CLI (Angular 4+) I installed both libraries and their typings using npm i: "dependencies": { "d3": "^4.10.2", "d3-cloud": "^1.2.4" }, "devDependencies": { "@types/d3": "^4.1 ...

Using a custom TypeScript wrapper for Next.js GetServerSideProps

I developed a wrapper for the SSR function GetServerSideProps to minimize redundancy. However, I am facing challenges in correctly typing it with TypeScript. Here is the wrapper: type WithSessionType = <T extends {}>( callback: GetServerSideProps&l ...

A class definition showcasing an abstract class with a restricted constructor access

Within my codebase, there is a simple function that checks if an object is an instance of a specific class. The function takes both the object and the class as arguments. To better illustrate the issue, here is a simplified example without delving into th ...

Angular components are experiencing issues with implementing Tailwind CSS

After incorporating Tailwind CSS into my Angular project, I noticed that it functions successfully at the root level of the project. However, when it comes to the SCSS files within individual components, it seems to be ineffective. Do I need to manually ...

Ways to address memory leakage issues in Angular

Is there a way to manually optimize the Garbage Collector by forcefully pushing variables into it? For example, if we have a root or global level variable in an Angular component that is no longer needed when switching to another page, how can we ensure it ...

A guide on setting up a countdown timer in Angular 4 for a daily recurring event with the help of Rxjs Observable and the Async Pipe

I'm currently working on developing a countdown timer for a daily recurring event using Angular 4, RxJS Observables, and the Async Pipe feature. Let's take a look at my component implementation: interface Time { hours: number; minutes: numbe ...

What is the best way to define the type of an object in TypeScript when passing it to a function

I am currently working on a function that accepts an object of keys with values that have specific types. The type for one field is determined by the type of another field in the same object. Here is the code: // Consider this Alpha type and echo function. ...

Is there a way to specify object keys in alignment with a specific pattern that allows for a variety of different combinations

I am seeking a way to restrict an object to only contain keys that adhere to a specific pattern. The pattern I require is: "{integer}a+{integer}c". An example of how it would be structured is as follows: { "2a+1c": { // ... } } Is there a ...

The Angular 2 http request seems to be failing to reach the web api's get method when using a string parameter overload

Issue at hand is that the initial Get method gets triggered by this particular HTTP request: http://localhost:56690/api/testelements/?name=aeg One would anticipate the second (string overload) method to be invoked due to the presence of a string parameter ...

The object is given a value in the form of a string, even though it is a strongly-typed variable

I'm encountering something unusual in my code. In the following example, there is a (change) event that captures the current value selected by the user from a dropdown menu. <div style="padding-right: 0; width: 100%;"> <label st ...

What kind of function possesses additional attributes or characteristics?

I am finding it difficult to define the type for the function foo as demonstrated below: function foo() { do something } foo.boo = () => { do something else } foo("hello"); foo.boo("hello"); This JavaScript code is functioning corr ...

What is the best way to modify the quantity property of a cart item object within the framework of this Angular 13 online shopping application?

I am currently developing an e-commerce app with a front-end built using Angular 13. The following code snippet is designed to calculate the total price of items in the shopping cart: import { Component, OnInit } from '@angular/core'; @Componen ...

Finding the duration of an audio file in a React/Typescript environment

I've been attempting to determine the duration of an audio file. It seems like the audio property is not included in the file by default. The only properties I see are size, name, and type. Is there a way for me to get the duration of the audio file i ...

Should data objects be loaded from backend API all at once or individually for each object?

Imagine having a form used to modify customer information. This form includes various input fields as well as multiple dropdown lists for fields such as country, category, and status. Each dropdown list requires data from the backend in order to populate i ...

Angular-powered dynamic data table component fails to render data sources in display

Currently, I am deep in the process of mastering Angular. I've been developing a versatile table component that can be utilized across my entire application. I have successfully configured the columns and pagination settings. Everything looks correct ...

Develop a set of data series for HighCharts using JSON input

My task is to aggregate data for a HighCharts Column Chart based on the JSON provided below. The Y-Axis data in HighCharts should have 0 when there is no corresponding data for the X Series. Here's the initial JSON: { "groups": { "1567310 ...