mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2025-08-17 18:54:11 +08:00
fix(angular): add NavParams
This commit is contained in:
2811
angular/package-lock.json
generated
2811
angular/package-lock.json
generated
File diff suppressed because one or more lines are too long
45
angular/src/directives/navigation/nav-params.ts
Normal file
45
angular/src/directives/navigation/nav-params.ts
Normal file
@ -0,0 +1,45 @@
|
||||
/**
|
||||
* @name NavParams
|
||||
* @description
|
||||
* NavParams are an object that exists on a page and can contain data for that particular view.
|
||||
* Similar to how data was pass to a view in V1 with `$stateParams`, NavParams offer a much more flexible
|
||||
* option with a simple `get` method.
|
||||
*
|
||||
* @usage
|
||||
* ```ts
|
||||
* import { NavParams } from '@ionic/angular';
|
||||
*
|
||||
* export class MyClass{
|
||||
*
|
||||
* constructor(navParams: NavParams){
|
||||
* // userParams is an object we have in our nav-parameters
|
||||
* navParams.get('userParams');
|
||||
* }
|
||||
*
|
||||
* }
|
||||
* ```
|
||||
*/
|
||||
export class NavParams {
|
||||
|
||||
constructor(public data: {[key: string]: any} = {}) {}
|
||||
|
||||
/**
|
||||
* Get the value of a nav-parameter for the current view
|
||||
*
|
||||
* ```ts
|
||||
* import { NavParams } from 'ionic-angular';
|
||||
*
|
||||
* export class MyClass{
|
||||
* constructor(public navParams: NavParams){
|
||||
* // userParams is an object we have in our nav-parameters
|
||||
* this.navParams.get('userParams');
|
||||
* }
|
||||
* }
|
||||
* ```
|
||||
*
|
||||
* @param {string} param Which param you want to look up
|
||||
*/
|
||||
get(param: string): any {
|
||||
return this.data[param];
|
||||
}
|
||||
}
|
@ -1,5 +1,6 @@
|
||||
import { ApplicationRef, ComponentFactoryResolver, Injectable, Injector, NgZone } from '@angular/core';
|
||||
import { ApplicationRef, ComponentFactoryResolver, Injectable, InjectionToken, Injector, NgZone } from '@angular/core';
|
||||
import { FrameworkDelegate, ViewLifecycle } from '@ionic/core';
|
||||
import { NavParams } from '../directives/navigation/nav-params';
|
||||
|
||||
|
||||
@Injectable()
|
||||
@ -58,14 +59,14 @@ export function attachView(
|
||||
injector: Injector,
|
||||
appRef: ApplicationRef,
|
||||
elRefMap: WeakMap<HTMLElement, any>,
|
||||
container: any, component: any, data?: any, cssClasses?: string[]) {
|
||||
container: any, component: any, params?: any, cssClasses?: string[]) {
|
||||
const componentFactory = cfr.resolveComponentFactory(component);
|
||||
const hostElement = document.createElement(componentFactory.selector);
|
||||
if (data) {
|
||||
Object.assign(hostElement, data);
|
||||
if (params) {
|
||||
Object.assign(hostElement, params);
|
||||
}
|
||||
|
||||
const childInjector = Injector.create([], injector);
|
||||
const childInjector = Injector.create(getProviders(params), injector);
|
||||
const componentRef = componentFactory.create(childInjector, [], hostElement);
|
||||
for (const clazz of cssClasses) {
|
||||
hostElement.classList.add(clazz);
|
||||
@ -95,3 +96,21 @@ export function bindLifecycleEvents(instance: any, element: HTMLElement) {
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
const NavParamsToken = new InjectionToken<any>('NavParamsToken');
|
||||
|
||||
|
||||
function getProviders(params: {[key: string]: any}) {
|
||||
return [
|
||||
{
|
||||
provide: NavParamsToken, useValue: params
|
||||
},
|
||||
{
|
||||
provide: NavParams, useFactory: provideNavParamsInjectable, deps: [NavParamsToken]
|
||||
}
|
||||
];
|
||||
}
|
||||
|
||||
function provideNavParamsInjectable(params: {[key: string]: any}) {
|
||||
return new NavParams(params);
|
||||
}
|
||||
|
Reference in New Issue
Block a user