Skip to content

Commit 4640e04

Browse files
feat(angular): setting props on a signal works (#29453)
Issue number: resolves #28876 --------- <!-- Please do not submit updates to dependencies unless it fixes an issue. --> <!-- Please try to limit your pull request to one type (bugfix, feature, etc). Submit multiple pull requests if needed. --> ## What is the current behavior? <!-- Please describe the current behavior that you are modifying. --> When assigning `componentProps` as inputs to an Angular component, we do `Object.assign`. When using the newer Angular Signals API for inputs the value of an input is a function: ```js myInput = input<string>('foo') // this is a function ``` The developer accesses the value of `myInput` in a template by doing `myInput()` since `myInput` is a function. If a developer passes `componentProps: { myInput: 'bar' }` then the value of `myInput` is set to this string value, overriding the function. As a result, calling `myInput()` results in an error because `myInput` is a string not a function. ## What is the new behavior? <!-- Please describe the behavior or changes that are being added by this PR. --> - Angular 14.1 introduced `setInput` which lets us hand off setting inputs to Angular. This will set input values properly even when using a Signals-based input. ## Does this introduce a breaking change? - [x] Yes - [ ] No <!-- If this introduces a breaking change: 1. Describe the impact and migration path for existing applications below. 2. Update the BREAKING.md file with the breaking change. 3. Add "BREAKING CHANGE: [...]" to the commit description when merging. See https://github.com/ionic-team/ionic-framework/blob/main/.github/CONTRIBUTING.md#footer for more information. --> As part of this `NavParams` has been deprecated as it is incompatible with the `setInput` API. The old `Object.assign` worked to allow devs to get all of the `componentProp` key value pairs via `NavParams` even if they are not defined as `Inputs`. Using `setInput` will now throw an error, so developers need to create an `@Input` for each parameter. This means that `NavParams` has no purpose and can safely be retired in favor of Angular's Input API. Not removing NavParms would make it difficult for us to support new Angular APIs such as this Signals-based input API. ## Other information <!-- Any other information that is important to this PR such as screenshots of how the component looks before and after the change. --> Dev build: `8.1.1-dev.11715021973.16675b67` You will need to update the Ionic config to opt-in to the new option: ```ts useSetInputAPI: true, ``` --------- Co-authored-by: Liam DeBeasi <[email protected]>
1 parent 0124f3b commit 4640e04

File tree

10 files changed

+76
-28
lines changed

10 files changed

+76
-28
lines changed

packages/angular/common/src/directives/navigation/nav-params.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@
1919
* ```
2020
*/
2121
export class NavParams {
22-
constructor(public data: { [key: string]: any } = {}) {}
22+
constructor(public data: { [key: string]: any } = {}) {
23+
console.warn(
24+
`[Ionic Warning]: NavParams has been deprecated in favor of using Angular's input API. Developers should migrate to either the @Input decorator or the Signals-based input API.`
25+
);
26+
}
2327

2428
/**
2529
* Get the value of a nav-parameter for the current view

packages/angular/common/src/providers/angular-delegate.ts

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,15 @@ import {
2020

2121
import { NavParams } from '../directives/navigation/nav-params';
2222

23+
import { ConfigToken } from './config';
24+
2325
// TODO(FW-2827): types
2426

2527
@Injectable()
2628
export class AngularDelegate {
2729
private zone = inject(NgZone);
2830
private applicationRef = inject(ApplicationRef);
31+
private config = inject(ConfigToken);
2932

3033
create(
3134
environmentInjector: EnvironmentInjector,
@@ -37,7 +40,8 @@ export class AngularDelegate {
3740
injector,
3841
this.applicationRef,
3942
this.zone,
40-
elementReferenceKey
43+
elementReferenceKey,
44+
this.config.useSetInputAPI ?? false
4145
);
4246
}
4347
}
@@ -51,7 +55,8 @@ export class AngularFrameworkDelegate implements FrameworkDelegate {
5155
private injector: Injector,
5256
private applicationRef: ApplicationRef,
5357
private zone: NgZone,
54-
private elementReferenceKey?: string
58+
private elementReferenceKey?: string,
59+
private enableSignalsSupport?: boolean
5560
) {}
5661

5762
attachViewToDom(container: any, component: any, params?: any, cssClasses?: string[]): Promise<any> {
@@ -84,7 +89,8 @@ export class AngularFrameworkDelegate implements FrameworkDelegate {
8489
component,
8590
componentProps,
8691
cssClasses,
87-
this.elementReferenceKey
92+
this.elementReferenceKey,
93+
this.enableSignalsSupport
8894
);
8995
resolve(el);
9096
});
@@ -121,7 +127,8 @@ export const attachView = (
121127
component: any,
122128
params: any,
123129
cssClasses: string[] | undefined,
124-
elementReferenceKey: string | undefined
130+
elementReferenceKey: string | undefined,
131+
enableSignalsSupport: boolean | undefined
125132
): any => {
126133
/**
127134
* Wraps the injector with a custom injector that
@@ -164,7 +171,38 @@ export const attachView = (
164171
);
165172
}
166173

167-
Object.assign(instance, params);
174+
/**
175+
* Angular 14.1 added support for setInput
176+
* so we need to fall back to Object.assign
177+
* for Angular 14.0.
178+
*/
179+
if (enableSignalsSupport === true && componentRef.setInput !== undefined) {
180+
const { modal, popover, ...otherParams } = params;
181+
/**
182+
* Any key/value pairs set in componentProps
183+
* must be set as inputs on the component instance.
184+
*/
185+
for (const key in otherParams) {
186+
componentRef.setInput(key, otherParams[key]);
187+
}
188+
189+
/**
190+
* Using setInput will cause an error when
191+
* setting modal/popover on a component that
192+
* does not define them as an input. For backwards
193+
* compatibility purposes we fall back to using
194+
* Object.assign for these properties.
195+
*/
196+
if (modal !== undefined) {
197+
Object.assign(instance, { modal });
198+
}
199+
200+
if (popover !== undefined) {
201+
Object.assign(instance, { popover });
202+
}
203+
} else {
204+
Object.assign(instance, params);
205+
}
168206
}
169207
if (cssClasses) {
170208
for (const cssClass of cssClasses) {

packages/angular/src/ionic-module.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,18 @@ const DECLARATIONS = [
5252
IonMaxValidator,
5353
];
5454

55+
type OptInAngularFeatures = {
56+
useSetInputAPI?: boolean;
57+
};
58+
5559
@NgModule({
5660
declarations: DECLARATIONS,
5761
exports: DECLARATIONS,
58-
providers: [AngularDelegate, ModalController, PopoverController],
62+
providers: [ModalController, PopoverController],
5963
imports: [CommonModule],
6064
})
6165
export class IonicModule {
62-
static forRoot(config?: IonicConfig): ModuleWithProviders<IonicModule> {
66+
static forRoot(config: IonicConfig & OptInAngularFeatures = {}): ModuleWithProviders<IonicModule> {
6367
return {
6468
ngModule: IonicModule,
6569
providers: [
@@ -73,6 +77,7 @@ export class IonicModule {
7377
multi: true,
7478
deps: [ConfigToken, DOCUMENT, NgZone],
7579
},
80+
AngularDelegate,
7681
provideComponentInputBinding(),
7782
],
7883
};

packages/angular/standalone/src/providers/ionic-angular.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ import type { IonicConfig } from '@ionic/core/components';
88
import { ModalController } from './modal-controller';
99
import { PopoverController } from './popover-controller';
1010

11-
export const provideIonicAngular = (config?: IonicConfig): EnvironmentProviders => {
11+
type OptInAngularFeatures = {
12+
useSetInputAPI?: boolean;
13+
};
14+
15+
export const provideIonicAngular = (config: IonicConfig & OptInAngularFeatures = {}): EnvironmentProviders => {
1216
return makeEnvironmentProviders([
1317
{
1418
provide: ConfigToken,

packages/angular/test/apps/ng16/src/app/lazy/version-test/modal-nav-params/nav-root.component.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { JsonPipe } from "@angular/common";
2-
import { Component } from "@angular/core";
2+
import { Component, Input } from "@angular/core";
33

44
import { IonicModule } from "@ionic/angular";
55

@@ -23,7 +23,7 @@ let rootParamsException = false;
2323
})
2424
export class NavRootComponent {
2525

26-
params: any;
26+
@Input() params: any = {};
2727

2828
ngOnInit() {
2929
if (this.params === undefined) {

packages/angular/test/apps/ng17/src/app/lazy/version-test/modal-nav-params/nav-root.component.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { JsonPipe } from "@angular/common";
2-
import { Component } from "@angular/core";
2+
import { Component, Input } from "@angular/core";
33

44
import { IonicModule } from "@ionic/angular";
55

@@ -23,7 +23,7 @@ let rootParamsException = false;
2323
})
2424
export class NavRootComponent {
2525

26-
params: any;
26+
@Input() params: any;
2727

2828
ngOnInit() {
2929
if (this.params === undefined) {

packages/angular/test/base/src/app/lazy/alert/alert.component.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { Component, NgZone } from '@angular/core';
22
import { AlertController } from '@ionic/angular';
3-
import { NavComponent } from '../nav/nav.component';
43

54
@Component({
65
selector: 'app-alert',

packages/angular/test/base/src/app/lazy/modal-example/modal-example.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<ion-content class="ion-padding">
1212
<h1>Value</h1>
1313
<h2>{{value}}</h2>
14-
<h3>{{valueFromParams}}</h3>
14+
<h3>{{prop}}</h3>
1515
<p>modal is defined: <span id="modalInstance">{{ !!modal }}</span></p>
1616
<p>ngOnInit: <span id="ngOnInit">{{onInit}}</span></p>
1717
<p>ionViewWillEnter: <span id="ionViewWillEnter">{{willEnter}}</span></p>

packages/angular/test/base/src/app/lazy/modal-example/modal-example.component.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Component, Input, NgZone, OnInit, Optional } from '@angular/core';
22
import { UntypedFormControl, UntypedFormGroup } from '@angular/forms';
3-
import { ModalController, NavParams, IonNav, ViewWillLeave, ViewDidEnter, ViewDidLeave } from '@ionic/angular';
3+
import { ModalController, IonNav, ViewWillLeave, ViewDidEnter, ViewDidLeave } from '@ionic/angular';
44

55
@Component({
66
selector: 'app-modal-example',
@@ -9,12 +9,12 @@ import { ModalController, NavParams, IonNav, ViewWillLeave, ViewDidEnter, ViewDi
99
export class ModalExampleComponent implements OnInit, ViewWillLeave, ViewDidEnter, ViewWillLeave, ViewDidLeave {
1010

1111
@Input() value?: string;
12+
@Input() prop?: string;
1213

1314
form = new UntypedFormGroup({
1415
select: new UntypedFormControl([])
1516
});
1617

17-
valueFromParams: string;
1818
onInit = 0;
1919
willEnter = 0;
2020
didEnter = 0;
@@ -25,11 +25,8 @@ export class ModalExampleComponent implements OnInit, ViewWillLeave, ViewDidEnte
2525

2626
constructor(
2727
private modalCtrl: ModalController,
28-
@Optional() public nav: IonNav,
29-
navParams: NavParams
30-
) {
31-
this.valueFromParams = navParams.get('prop');
32-
}
28+
@Optional() public nav: IonNav
29+
) {}
3330

3431
ngOnInit() {
3532
NgZone.assertInAngularZone();
Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import { Component } from '@angular/core';
1+
import { Component, Input } from '@angular/core';
22
import { ModalExampleComponent } from '../modal-example/modal-example.component';
3-
import { NavParams } from '@ionic/angular';
43

54
@Component({
65
selector: 'app-nav',
@@ -10,11 +9,13 @@ export class NavComponent {
109
rootPage = ModalExampleComponent;
1110
rootParams: any;
1211

13-
constructor(
14-
params: NavParams
15-
) {
12+
@Input() value?: string;
13+
@Input() prop?: string;
14+
15+
ngOnInit() {
1616
this.rootParams = {
17-
...params.data
17+
value: this.value,
18+
prop: this.prop
1819
};
1920
}
2021
}

0 commit comments

Comments
 (0)