Utilizing and Testing Input Signals in Angular 17.1

Posted by


In Angular 17.1, input signals are a powerful feature that allows you to pass data from a parent component to a child component. This is extremely useful when you have data that needs to be shared across different components within your application. In this tutorial, we will walk through how to use input signals in Angular and how to test them to ensure they are functioning correctly.

  1. Setting up Input Signals in Angular:

To set up input signals in Angular, you first need to define an input property on the child component that will receive the data. This can be done by using the @Input() decorator in the child component class. For example:

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

@Component({
  selector: 'app-child',
  templateUrl: './child.component.html',
  styleUrls: ['./child.component.css']
})
export class ChildComponent {
  @Input() data: any;
}

In this example, we have defined an input property called ‘data’ in the ChildComponent class. This property will be used to receive data from the parent component.

  1. Passing data from the parent component to the child component:

To pass data from the parent component to the child component, you need to bind the data to the input property in the child component using property binding. This can be done by using square brackets in the HTML template of the parent component. For example:

<app-child [data]="parentData"></app-child>

In this example, we are binding the ‘parentData’ property in the parent component to the ‘data’ input property in the child component. This will pass the value of ‘parentData’ to the ‘data’ input property in the child component.

  1. Testing Input Signals in Angular:

To test input signals in Angular, you can create a test case using a testing framework like Jasmine or Jest. In the test case, you can set the input property of the child component and then assert that the data has been passed correctly.

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

describe('ChildComponent', () => {
  it('should pass data from parent component to child component', () => {
    TestBed.configureTestingModule({
      declarations: [ChildComponent],
    });

    const fixture = TestBed.createComponent(ChildComponent);
    const childComponent = fixture.componentInstance;

    const testData = 'test data';
    childComponent.data = testData;

    fixture.detectChanges();

    expect(childComponent.data).toEqual(testData);
  });
});

In this test case, we are setting the ‘data’ input property of the ChildComponent to ‘test data’ and then asserting that the value of ‘data’ is equal to ‘test data’. This test case ensures that input signals are working correctly and that data is being passed from the parent component to the child component.

In conclusion, input signals are a powerful feature in Angular that allows you to pass data from a parent component to a child component. By following the steps outlined in this tutorial, you can successfully use and test input signals in your Angular application.

0 0 votes
Article Rating

Leave a Reply

20 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@DecodedFrontend
24 days ago

Learn more about RxDB – https://bit.ly/rxdb 🚀

@Storiada
24 days ago

Thank you! Finally, a clear and transparent explanation of how Signals work, really emphasising their benefits and practical use. I’m already on the fourth video and loving it—your content is making my life so much easier, even though I’ve been working with Angular since its early days.

Great job, and the videos are so good produced and organised! Maybe a bit of spark in your explanation would make them even more better, cause you are really focus on the content and might be detaching to have a bit of a joke or a smile.

Very much appreciate for the work involved in producing this videos.

@tangocukedi1
24 days ago

after doing this refactoring, unit testing has become a nightmare for me because i use jest without Testbed

@rkrao8582
24 days ago

I am confused a bit . You mentioned that passing signals from parent to child as inputs is an anti pattern, But I dont understand why. I am working on an application with Angular 18 and I am passing signals as inputs from parent to child. Dont know what I am missing here! But great video none the less

@gabo868
24 days ago

Thanks!

@ВладимирФомин-с4б
24 days ago

Angular signals resemble MobX 🤔

@nelson3391
24 days ago

Great video. Thanks a lot!

@nergrohombre
24 days ago

I had problems with the required input and had to set it with spectator createComponent props

@deeperton
24 days ago

Breaking contracts of using decorators is so React-way… don't like it. Now you need a custom IDE tuning to highlight that input function to make it noticeable in the code.

@kiambojyms2196
24 days ago

Js frameworks are sharing syntaxes. Computed comes from vuejs.

@asakurayoh3909
24 days ago

Angular is so much better when it comes to complex, large applications. Now with all these new features, it's killing it. The more I learn about these new features the more I'm in love with Angular ❤

@dharmarajana3989
24 days ago

We can achieve the same using behavioural subjects..why do we need signals?

@snivels
24 days ago

So what is the point of signals? Is it to replace the use of ngModel two-way bindings?

@VK-sn3sn
24 days ago

Hello, what are your thoughts on OnPush Change detection strategy? Is it better to make my whole web app using this strategy only?

@fawzzhkz9023
24 days ago

@itslen
24 days ago

Is it possible to use input signals with the router?

@tomasroggero6559
24 days ago

How do you test outputs?? such as model()

@PavelBozhok-d4g
24 days ago

Hello, the video is awesome! Maybe you know how to set data to input signal from another component? What do I mean, for example

In LabelComponent I have InputSignal
required: InputSignal<boolean> = input<boolean>(false);

I have a parent component ParentComponent
protected _labelRef contentChild(LabelComponent);

and code
this._labelRef().required.set(false); // it not possible because InputSignal don't have set method

Maybe you know a variant to do that with signal, I know how to do that from the old way, but try to use the signal for new features, thanks a lot!

@PavelBozhok-d4g
24 days ago

Awesome video! Maybe you know how to set input from another component? for example

protected _labelRef = contentChild(LabelComponent)

this._labelRef().required.set(true); not possible, because

@Aliakbaresmaeiliiii
24 days ago

thank you man

Im a Angualr developer over 7 year but every time i learning new stuff from you

thank you very much man

god bluss you

20
0
Would love your thoughts, please comment.x
()
x