Unlocking the Power of Data Passing Through Tabs

My Application Structure

Client Module - Material Tab

  • Tab 1 - Customer Table View
  • Tab 2 - Edit Customer View
<mat-tab-group>
  <mat-tab label="View">
    <div>
      <app-customer-table></app-customer-table>
    </div>
  </mat-tab>
  <mat-tab label="Edit">
    <div>
      <app-customer-create></app-customer-create>
    </div>
  </mat-tab>
</mat-tab-group>


When the "edit" icon within Tab 1 is clicked, I aim to transition to Tab 2 and transmit my "customer:CustomerDTO" data.

What steps should I take to accomplish this?

Answer №1

To exchange data between components in Angular, you can utilize @Output in the sending component to store a value in a variable and then use @Input in the receiving component to access that variable.

Here is an example of how it can be implemented:

<mat-tab-group>
        <mat-tab label="View">
            <div>
                <app-customer-table (onClickEdit)="edit($event)"></app-customer-table>
            </div>
        </mat-tab>
        <mat-tab label="Edit">
            <div>
                <app-customer-create [customerId]="customerId"></app-customer-create>
            </div>
        </mat-tab>
    </mat-tab-group>

    // main-componet.ts file

    edit(value)=>{
        this.customerId = value;
    }

    // customer-create.component.ts

    @Input CustomerId;

    // customer-table.component.ts
    @Output onClickEdit = new EventEmitter<string>();
    

Answer №2

To accomplish this task, follow these steps:

  1. Begin by creating a service using the following command:
ng generate service data-handling
  1. Within that service, define two variables to hold the data for each component:
import {Injectable} from '@angular/core';

@Injectable({
  providedIn: 'root',
})
export class DataHandlingService {
  public firstTabData;
  public secondTabData;

  constructor() { }

}
  1. Implement the service in your component:
import {DataHandlingService} from '...';

...
constructor(public dataHandlingService: DataHandlingService) {
}
  1. Assign the data for each component to their respective variables:
setData(first, second) {
  this.dataHandlingService.firstTabData = first;
  this.dataHandlingService.secondTabData = second;
}
  1. Utilize the desired data in the tabs

You also have the option to create a data variable and store the information there instead of using separate variables

Answer №3

If you need to pass data to a component, there are a few ways you can do it. One option is to use a service, while another is to utilize property binding and event binding syntax like so:

For tab1, you can employ an EventEmitter. When the user clicks on edit, call the emit function to send over the necessary data.
By using event binding, you can then retrieve this data in the parent component and transfer it to the child component through property binding.

You can find more information here:

To switch tabs, access the <mat-tab-group> in your component.ts file by using the @ViewChild decorator and adjust the selectedIndex property accordingly.

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

Issue with Angular Material sorting (matSort) triggering ExpressionChangedAfterItHasBeenCheckedError

I have limited experience with JS, Angular, and even Node.js, but I needed to create a user interface for an application I am developing. I am learning as I go along and have managed to solve most issues on my own. However, I am currently facing an Express ...

The properties required by the type for typescript reactjs are not present

I've come across an array with the following structure: export const SideBarTags = [ { name: 'Tutorials', link: '../tutorials', icon: faFileAlt, dropdownItems: null, active: false, }, { name: 'An ...

Issue with Ionic 4 IOS deeplinks: Instead of opening in the app, they redirect to the browser

After working diligently to establish deeplinks for my Ionic 4 iOS application, I meticulously followed a series of steps to achieve this goal: I uploaded an Apple site association file to the web version of the app, ensuring the utilization of the prec ...

Challenge with using the React useEffect hook

Incorporating the React useEffect hook into my code has been a bit challenging. Here is how I am attempting to use it: function App() { React.useEffect(() => { console.log('effect working') }, []) return ( <div className=" ...

Pass down properties to a React component

Currently, all my SVCs are created as react components and I would like the ability to pass a color attribute as a prop to override the default color of the components. However, using props attribute results in an unattractive object that needs to be defin ...

Adjust the dimensions of an Angular Material 2 dialog by updating the width or height

Is there a way to adjust the dimensions of an open Angular Material 2 dialog, either its width or height? I attempted to modify the size of the dialog by obtaining a reference to it and using the updateSize method within the dialog. Unfortunately, I belie ...

Guide to connecting data from the backend to the frontend in the select option feature using Angular 9

I have a backend system where I store a number representing a selected object, which I am trying to display in a select option using Angular. Currently, the select option only displays a list of items that I have predefined in my TypeScript code using enu ...

Issue with Angular Reactive form: Checkbox checked property not binding correctly when the page initially loads

Looking to achieve Two-way data binding of Checkbox in Angular Reactive forms. After checking the checkbox, I am updating the 'isdateChkd' variable and storing it in the state. Despite the variable being set to TRUE, the checkbox does not get aut ...

Display the data in ngx-charts heat map

I have been utilizing ngx charts to display data in a Heat Map. The implementation is smooth without encountering any issues. View Working Example | Heat Map However, I am interested in displaying the values of each grid rather than just on mouse hover. ...

Is there a way to trigger the click event in the week view of an Angular 2+ calendar?

https://i.sstatic.net/Vx2x8.png HTML Templates <mwl-calendar-week-view [viewDate]="viewDate" [refresh]="refresh" (click)="weekDayClick($event)"> </mwl-calendar-week-view> In the component file weekDayCl ...

The content displayed in the PrimeNG p-table is limited to only the table name with no additional information

After upgrading Angular to version 9, I switched from p-dataTable to p-table in PrimeNG. With a table named users, I intended to display them on the screen upon rendering the view using the following HTML: users = ['one','two','thr ...

Auto increment package.json version in a monorepo configuration

I am working on an Angular 6 app configured as a monorepo, comprising of a project that needs to be published to NPM along with a demo app. To update the versions of both the application and the project, I would like to utilize the npm version command. Th ...

What is the best method for choosing visible elements within a scrollable container?

Is there a way to retrieve the list of visible elements within a scrollable container? The number of elements that are visible on the screen changes as one scrolls, making it challenging to add a specific class to only the last two visible elements. Any s ...

Angular Universal is experiencing difficulties resolving dependencies

After finally migrating my existing Angular project from v8 to v13.0.0, I decided to incorporate SSR into it. The process of updating the project itself was time-consuming and challenging. Once the app successfully ran on v13.0.0, I attempted to add SSR b ...

Use ngFor to filter content from another ngFor loop

I am in the process of creating a mobile application that involves filtering students based on their classes assigned by a teacher. Initially, I use ngFor to display all the classes taught by the teacher in a div, and then I aim to list the students enroll ...

Angular-cli does not come with the Bootstrap framework pre-installed

After installing the bootstrap framework to my angular2 project through npm, I noticed that it appeared in the node-modules folder. However, upon checking the angular-cli file, I discovered that the boostrap script and css were not included. "apps": [ ...

Best practice for validating a form using React: Why the state doesn't update immediately with useState and onSubmit

I'm currently working on implementing a custom form validation for my React project using Typescript. However, I've encountered an issue with the useState hook not updating the state containing errors immediately upon form submission. Let me illu ...

Encountering issues with package resolution in VS Code while setting up a monorepo with yarn workspaces, Vite, React, and

I recently set up a monorepo using yarn@3 workspaces. Here is the structure of my root package.json: { "name": "hello-yarn-workspaces", "packageManager": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" ...

Issue with intrinsic attributes detected in Typescript for the component

Hey, I'm encountering an issue that says, "The type '{ data: dataProp[]; }' cannot be assigned to type 'IntrinsicAttributes & dataProp'. A property 'data' does not exist on type 'IntrinsicAttributes & dataPro ...

Changing the button class during an event in Angular 4

In the process of creating an MCQ test, I am looking to implement a feature where selected button options are highlighted in green upon clicking. While I have successfully implemented this feature using Angular 1, I am facing challenges in converting it to ...