Sending data to child component in Angular 2

Is there a way to seamlessly pass attributes from wrapper component to nested component?

When we have

const FIRST_PARTY_OWN_INPUTS = [...];
const FIRST_PARTY_PASSTHROUGH_INPUTS = ['all', 'attrs', 'are', 'passed'];
@Component({
  selector: 'first-party',
  inputs: [...FIRST_PARTY_OWN_INPUTS, ...FIRST_PARTY_PASSTHROUGH_INPUTS],
  template: `
<div>
  <third-party [all]="all" [attrs]="attrs" [are]="are" [passed]="passed"></third-party>
  <first-party-extra></first-party-extra>
</div>
  `,
  directives: [ThirdParty]
})
export class FirstParty { ... }

Could the inputs be translated collectively without listing them in the template?

The provided code aims to replicate Angular 1.x directive functionality:

app.directive('firstParty', function (thirdPartyDirective) {
  const OWN_ATTRS = [...];
  const PASSTHROUGH_ATTRS = Object.keys(thirdPartyDirective[0].scope);

  return {
    scope: ...,
    template: `
<div>
  <third-party></third-party>
  <first-party-extra></first-party-extra>
</div>
    `,
compile: function (element, attrs) {
      const nestedElement = element.find('third-party');

      for (let [normalizedAttr, attr] of Object.entries(attrs.$attr)) {
        if (PASSTHROUGH_ATTRS.includes(normalizedAttr)) {
          nestedElement.attr(attr, normalizedAttr);
        }
      }
    },
    ...
  };
});

Answer №1

I'm not entirely certain if my implementation is correct, but here it is ( PLUNKER )


const OWN_INPUTS = ['not', 'passthrough'];
const PASSTHROUGH_INPUTS = ['all', 'attrs', 'are', 'passed'];

const generateAttributes(arr) {
   return arr.map(att => '[' + att + '] = "' + att + '"').join(' ');
}


//-------------------------------------------------------//////////////////
import {Component} from '@angular/core'

@Component({
  selector: 'third-party',
  inputs: [...PASSTHROUGH_INPUTS],
  template: `
<div>
  {{all}} , {{attrs}} ,  {{are}} ,  {{passed}}
</div>
  `
})
export class ThirdParty {
}

@Component({
  selector: 'first-party',
  inputs: [...OWN_INPUTS, ...PASSTHROUGH_INPUTS],
  template: `
<div>
  <div>
    {{not}} , {{passthrough}}
  </div>
  <third-party ${generateAttributes(PASSTHROUGH_INPUTS)}></third-party>
  <first-party-extra></first-party-extra>
</div>
  `,
  directives: [ThirdParty]
})
export class FirstParty {
}

@Component({
  selector: 'my-app',
  providers: [],
  template: `
    <div>
      <h2>Hello {{name}}</h2>
      <first-party [not]="'not'" [passthrough]="'passthrough'"  
                   [all]="'all'" [attrs]="'attrs'" [are]="'are'" [passed]="'passed'">
      </first-party>
    </div>
  `,
  directives: [FirstParty]
})
export class App {
  constructor() {
    this.name = 'Angular2 (Release Candidate!)'
  }
}

I hope this helps :)

Answer №2

Reducing this issue to its core, it is clear that dealing with functions that require numerous parameters can be cumbersome and prone to errors, even without the complexity of Angular2. The dilemma intensifies when there is an intermediate function that has no use for these parameters, forcing them to be passed through unnecessarily. It can become quite frustrating!

To address this challenge, various approaches can be taken. One effective method is to pre-instantiate the inner function and pass it along already equipped with the necessary parameters encapsulated within. A helpful resource on achieving this in Angular 2 using @ViewChild and @ContentChild can be found at . Another strategy involves bundling all pass-through parameters into a single object, simplifying the process by reducing the number of individual parameters that need to be transferred. This approach also streamlines any future additions of parameters since they are already bundled together seamlessly.

Answer №3

To achieve this functionality, utilize the @Input() decorator within your child components.

Link to example

Parent Component:

import {Component} from '@angular/core';
import {ChildComponent} from './child.component';

@Component({
  selector: 'my-parent',
  directives: [ChildComponent],
  template: `
    <div>
      <h2>I am the parent.</h2>
      My name is {{firstName}} {{lastName}}.

        <my-child firstName="{{firstName}}" 
                  lastName="{{lastName}}">

        </my-child>

    </div>
  `
})
export class ParentComponent {
  public firstName:string;
  public lastName: string;
  constructor() {
    this.firstName = 'Alice';
    this.lastName = 'Johnson';
  }
}

Child Component:

import {Component, Input} from '@angular/core';

@Component({
  selector: 'my-child',
  template: `
    <div>
      <h3>I am the child.</h3>
      My name is {{firstName}} {{lastName}} Jr.
      <br/>
     The name I received from my parent is: {{firstName}} {{lastName}}

    </div>
  `
})
export class ChildComponent {
  @Input() firstName: string;
  @Input() lastName: string;
}

App Component:

//our root app component
import {Component} from '@angular/core';
import {ParentComponent} from './parent.component';

@Component({
  selector: 'my-app',
  directives: [ParentComponent],
  template: `
    <div>
      <my-parent></my-parent>
    </div>
  `
})
export class App {

  constructor() {
  }
}

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

The process of subscribing to a service in Angular

I currently have 3 objects: - The initial component - A connection service - The secondary component When the initial component is folded/expanded, it should trigger the expansion/folding of the secondary component through the service. Within the service ...

Angular2: Error - trying to access 'this.' which is not defined

I have a function that is designed to retrieve and display the "best player" from an array of objects, which essentially refers to the player with the most likes. The functionality of this function works as intended and displays the desired output. However ...

Creating categories in AngularJS using an array

<table class="table-striped table-bordered table-condensed"> <tbody> <tr ng-if="$index%3==0" ng-repeat="permission in vm.parent.getAllPermissions()"> <td ng-repeat="i in [0,1,2]" class="col-xs-2"> ...

Leverage information stored in an array within the HandsonTable Angular directive

Some of my columns in a HandsoneTable created using Angular directives are not rendering when I try to use an array as the data source with common array notation (name[0]). I'm unsure if this is supposed to work like this or if I am doing something wr ...

Discover the method for converting a local file into a string

Looking to retrieve and read an SVG file in Angular 6 from the assets folder as a string. I've attempted the following: this._htmlClient.get('../../assets/images/chart1.svg').subscribe(data => { console.log('svg-file: ', ...

Cross-origin error triggered by using an external API within a ReactJS application

I'm facing a common CORS issue in my application while trying to call an API. The API I am attempting to access can be found here: https://github.com/nickypangers/passport-visa-api Below is the code snippet used for making the API call: const getVi ...

Reset button in AngularJs fails to reset the $error.required message

<div class="form-group"> <input type="text" name="Name" data-ng-model="panNumber" required> <p data-ng-show="loginForm.Name.$error.required && (loginForm.Name.$touched || submitted)" class="error-block">Please make s ...

Is it possible for ng-invalid parameters to function in the same way as a conditional ng-required?

This is my first time seeking assistance here, and I have provided detailed information to explain my issue. I am developing an HTML form that incorporates AngularJS, as I am still learning about this technology. The form involves mathematical calculation ...

Enter the spotlight with all the information you need about AngularJS

Can someone please help me figure out how to display the post->id with a fade in effect? I've tried searching for a solution but haven't had any luck. Your assistance would be greatly appreciated!!! *I apologize in advance if my language is n ...

Utilizing Nativescript Angular, personalize the styling by separating the SCSS file into two distinct platform-specific SCSS files labeled as .android.scss and .ios

After modifying my component to convert it into a nativescript angular platform specific SCSS, everything seems to be working fine. The button's background turns yellow on the android platform/simulator and green on IOS, as specified in the SCSS files ...

Delete the strings in the array once the checkbox is unchecked

I have some data for my checkbox here, $scope.dataList = { 'ICT': true, 'OTTIX_DT': true, 'CP EMR AD': true, } <input type="checkbox" ng-model="miniAddons.dataList[data.jobName]" ng-change=& ...

Customize the template of a third-party component by overriding or extending it

I am facing a situation where I need to customize the template of a third party component that I have imported. However, since this component is part of an npm package, I want to avoid making direct changes to it in order to prevent issues when updating th ...

The click event is activated following the :active selector being triggered

My Angular application features a button with a slight animation - it moves down by a few pixels when clicked on: &:active { margin: 15px 0 0 0; } The button also has a (click)="myFunction()" event listener attached to it. A common issue arises w ...

Tips for locating the specific website address on a publicly accessible webpage

I am looking to receive notifications whenever a company's public website adds a document to their site. This needs to be done for approximately 400 different public sites. Since each site will have a unique document directory, I plan to create a data ...

Generating Angular 2 components on the fly using ngFor

A challenge I am facing is with my controller, which contains a list of components that I want to display within a page. These components are functioning correctly when used individually. Within my HTML template: <ng-container *ngFor="let component of ...

Why isn't the array loading upon the initial click in AngularJS?

// CONFIGURING SERVICES app.service("PictureService", function($http) { var Service = {}; Service.pictureLinkList = []; // RETRIEVING PICTURE LINKS FOR THE PAGE $http.get("data/img_location.json") .success(function(data) { Service.pictureLinkLi ...

Utilizing AngularJS to render nested array objects within HTML attributes

I have some data that looks like this: Continent":"Africa", "Countries":{ "Nigeria":[ { "Id":"2", "ColumnName":"Lagos", "Type":"Textbox", } ] } I am wondering if it i ...

Searching in Ruby on Rails using OR logic

Hello there! Currently, I am utilizing AngularJS alongside Ruby on Rails for the backend. I am curious about how to utilize the find method to search for an object array using one of two specific words. console.log(_($scope.reasonOfRejection).find({nam ...

Update the bower library by specifying the branch

Currently, I am utilizing the angular-file-upload directive for file uploads in my application. While I am currently using version 1.1.5 of this directive, I would like to update it to version 1.1.6. The challenge is that the master branch is now at vers ...

The data within an isolated scope is failing to appear in the template of the directive

I am currently working on a custom Angular directive that is designed to showcase pagination throughout my application. Despite using an isolated scope to pass the parameter totalNoOfRecords, I am facing issues with it not being displayed. Any assistance ...