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.
- 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.
- 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.
- 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.
Learn more about RxDB – https://bit.ly/rxdb 🚀
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.
after doing this refactoring, unit testing has become a nightmare for me because i use jest without Testbed
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
Thanks!
Angular signals resemble MobX 🤔
Great video. Thanks a lot!
I had problems with the required input and had to set it with spectator createComponent props
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.
Js frameworks are sharing syntaxes. Computed comes from vuejs.
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 ❤
We can achieve the same using behavioural subjects..why do we need signals?
So what is the point of signals? Is it to replace the use of ngModel two-way bindings?
Hello, what are your thoughts on OnPush Change detection strategy? Is it better to make my whole web app using this strategy only?
❤
Is it possible to use input signals with the router?
How do you test outputs?? such as model()
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!
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
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