GeezerVison is showing github.com — feat(core): add ability to set inputs on ComponentRef by pkozlowski-opensource · Pull Request #46641 · angular/angular
feat(core): add ability to set inputs on ComponentRef
#46641
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and
privacy statement . We’ll occasionally send you account related emails.
Already on GitHub?
Sign in
to your account
Uh oh!
There was an error while loading. Please reload this page.
Show file tree
Hide file tree
Changes from all commits
Commits
File filter
Filter by extension
.json
(9)
.md
(1)
.ts
(5)
All 3 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Hide whitespace
Diff view
Hide whitespace
There are no files selected for viewing
Original file line number
Diff line number
Diff line change
Expand Up
@@ -235,6 +235,7 @@ export abstract class ComponentRef<C> {
abstract get instance(): C;
abstract get location(): ElementRef;
abstract onDestroy(callback: Function): void;
abstract setInput(name: string, value: unknown): void;
}
// @public
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -33,7 +33,7 @@
"cli-hello-world-lazy": {
"uncompressed": {
"runtime": 2835,
"main": 236657 ,
"main": 237313 ,
"polyfills": 33842,
"src_app_lazy_lazy_module_ts": 780
}
Expand Down
Expand Up
@@ -68,4 +68,4 @@
"bundle": 1214857
}
}
}
}
Original file line number
Diff line number
Diff line change
Expand Up
@@ -23,6 +23,16 @@ import {ViewRef} from './view_ref';
* @publicApi
*/
export abstract class ComponentRef<C> {
/**
* Updates a specified input name to a new value. Using this method will properly mark for check
* component using the `OnPush` change detection strategy. It will also assure that the
* `OnChanges` lifecycle hook runs when a dynamically created component is change-detected.
*
Comment thread
pkozlowski-opensource marked this conversation as resolved.
Resolved
Uh oh!
There was an error while loading. Please reload this page.
* @param name The name of an input.
* @param value The new value of an input.
*/
abstract setInput(name: string, value: unknown): void;
/**
* The host or anchor [element](guide/glossary#element) for this component instance.
*/
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -11,7 +11,7 @@ import {Injector} from '../di/injector';
import {InjectFlags} from '../di/interface/injector';
import {ProviderToken} from '../di/provider_token';
import {EnvironmentInjector} from '../di/r3_injector';
import {RuntimeError, RuntimeErrorCode} from '../errors';
import {formatRuntimeError, RuntimeError, RuntimeErrorCode} from '../errors';
import {Type} from '../interface/type';
import {ComponentFactory as viewEngine_ComponentFactory, ComponentRef as viewEngine_ComponentRef} from '../linker/component_factory';
import {ComponentFactoryResolver as viewEngine_ComponentFactoryResolver} from '../linker/component_factory_resolver';
Expand All
@@ -26,16 +26,18 @@ import {assertComponentType} from './assert';
import {createRootComponent, createRootComponentView, createRootContext, LifecycleHooksFeature} from './component';
import {getComponentDef} from './definition';
import {NodeInjector} from './di';
import {createLView, createTView, locateHostElement, renderView} from './instructions/shared';
import {reportUnknownPropertyError} from './instructions/element_validation';
import {createLView, createTView, initializeInputAndOutputAliases, locateHostElement, markDirtyIfOnPush, renderView, setInputsForProperty} from './instructions/shared';
import {ComponentDef} from './interfaces/definition';
import {TContainerNode, TElementContainerNode, TElementNode, TNode} from './interfaces/node';
import {PropertyAliasValue, TContainerNode, TElementContainerNode, TElementNode, TNode} from './interfaces/node';
import {RNode} from './interfaces/renderer_dom';
import {HEADER_OFFSET, LView, LViewFlags, TViewType} from './interfaces/view';
import {HEADER_OFFSET, LView, LViewFlags, TVIEW, TViewType} from './interfaces/view';
import {MATH_ML_NAMESPACE, SVG_NAMESPACE} from './namespaces';
import {createElementNode, writeDirectClass} from './node_manipulation';
import {extractAttrsAndClassesFromSelector, stringifyCSSSelectorList} from './node_selector_matcher';
import {enterView, leaveView} from './state';
import {setUpAttributes} from './util/attrs_utils';
import {stringifyForError} from './util/stringify_utils';
import {getTNode} from './util/view_utils';
import {RootViewRef, ViewRef} from './view_ref';
Expand Down
Expand Up
@@ -226,7 +228,6 @@ export class ComponentFactory<T> extends viewEngine_ComponentFactory<T> {
// Angular 5 reference: https://stackblitz.com/edit/lifecycle-hooks-vcref
component = createRootComponent(
componentView, this.componentDef, rootLView, rootContext, [LifecycleHooksFeature]);
renderView(rootTView, rootLView, null);
} finally {
leaveView();
Expand Down
Expand Up
@@ -275,6 +276,25 @@ export class ComponentRef<T> extends viewEngine_ComponentRef<T> {
this.componentType = componentType;
}
override setInput(name: string, value: unknown): void {
const inputData = this._tNode.inputs;
let dataValue: PropertyAliasValue|undefined;
if (inputData !== null && (dataValue = inputData[name])) {
const lView = this._rootLView;
setInputsForProperty(lView[TVIEW], lView, dataValue, name, value);
markDirtyIfOnPush(lView, this._tNode.index);
} else {
if (ngDevMode) {
const cmpNameForError = stringifyForError(this.componentType);
let message =
`Can't set value of the '${name}' input on the '${cmpNameForError}' component. `;
message += `Make sure that the '${
name}' property is annotated with @Input() or a mapped @Input('${name}') exists.`;
reportUnknownPropertyError(message);
Comment thread
pkozlowski-opensource marked this conversation as resolved.
Resolved
Uh oh!
There was an error while loading. Please reload this page.
}
}
}
override get injector(): Injector {
return new NodeInjector(this._tNode, this._rootLView);
}
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -207,6 +207,10 @@ export function handleUnknownPropertyError(
}
}
reportUnknownPropertyError(message);
}
export function reportUnknownPropertyError(message: string) {
if (shouldThrowErrorOnUnknownProperty) {
throw new RuntimeError(RuntimeErrorCode.UNKNOWN_BINDING, message);
} else {
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -912,7 +912,7 @@ function generatePropertyAliases(
* Initializes data structures required to work with directive inputs and outputs.
* Initialization is done for all directives matched on a given TNode.
*/
function initializeInputAndOutputAliases(tView: TView, tNode: TNode): void {
export function initializeInputAndOutputAliases(tView: TView, tNode: TNode): void {
ngDevMode && assertFirstCreatePass(tView);
const start = tNode.directiveStart;
Expand Down
Expand Up
@@ -1010,7 +1010,7 @@ export function elementPropertyInternal<T>(
}
/** If node is an OnPush component, marks its LView dirty. */
function markDirtyIfOnPush(lView: LView, viewIndex: number): void {
export function markDirtyIfOnPush(lView: LView, viewIndex: number): void {
ngDevMode && assertLView(lView);
const childComponentLView = getComponentLViewByIndex(viewIndex, lView);
if (!(childComponentLView[FLAGS] & LViewFlags.CheckAlways)) {
Expand Down
Expand Up
@@ -1067,6 +1067,7 @@ export function instantiateRootComponent<T>(tView: TView, lView: LView, def: Com
directiveIndex, rootTNode.directiveStart,
'Because this is a root component the allocated expando should match the TNode component.');
configureViewWithDirective(tView, rootTNode, lView, directiveIndex, def);
initializeInputAndOutputAliases(tView, rootTNode);
}
const directive =
getNodeInjectable(lView, tView, rootTNode.directiveStart, rootTNode as TElementNode);
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -932,6 +932,9 @@
{
"name": "initTNodeFlags"
},
{
"name": "initializeInputAndOutputAliases"
},
{
"name": "injectArgs"
},
Expand Down
Expand Up
@@ -1085,6 +1088,9 @@
{
"name": "markAsComponentHost"
},
{
"name": "markDirtyIfOnPush"
},
{
"name": "maybeWrapInNotSelector"
},
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -698,6 +698,9 @@
{
"name": "initTNodeFlags"
},
{
"name": "initializeInputAndOutputAliases"
},
{
"name": "injectArgs"
},
Expand Down
Expand Up
@@ -965,6 +968,9 @@
{
"name": "setInjectImplementation"
},
{
"name": "setInputsForProperty"
},
{
"name": "setInputsFromAttrs"
},
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -1028,6 +1028,9 @@
{
"name": "initTNodeFlags"
},
{
"name": "initializeInputAndOutputAliases"
},
{
"name": "injectArgs"
},
Expand Down
Expand Up
@@ -1199,6 +1202,9 @@
{
"name": "markAsComponentHost"
},
{
"name": "markDirtyIfOnPush"
},
{
"name": "markDuplicates"
},
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -992,6 +992,9 @@
{
"name": "initTNodeFlags"
},
{
"name": "initializeInputAndOutputAliases"
},
{
"name": "injectArgs"
},
Expand Down
Expand Up
@@ -1160,6 +1163,9 @@
{
"name": "markAsComponentHost"
},
{
"name": "markDirtyIfOnPush"
},
{
"name": "markDuplicates"
},
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -398,6 +398,12 @@
{
"name": "forwardRef"
},
{
"name": "generateInitialInputs"
},
{
"name": "generatePropertyAliases"
},
{
"name": "getClosureSafeProperty"
},
Expand Down
Expand Up
@@ -548,6 +554,9 @@
{
"name": "isImportedNgModuleProviders"
},
{
"name": "isInlineTemplate"
},
{
"name": "isLContainer"
},
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -1367,6 +1367,9 @@
{
"name": "initTNodeFlags"
},
{
"name": "initializeInputAndOutputAliases"
},
{
"name": "inject"
},
Expand Down
Expand Up
@@ -1811,6 +1814,9 @@
{
"name": "setInjectImplementation"
},
{
"name": "setInputsForProperty"
},
{
"name": "setInputsFromAttrs"
},
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -473,6 +473,12 @@
{
"name": "forwardRef"
},
{
"name": "generateInitialInputs"
},
{
"name": "generatePropertyAliases"
},
{
"name": "getClosureSafeProperty"
},
Expand Down
Expand Up
@@ -632,6 +638,9 @@
{
"name": "isImportedNgModuleProviders"
},
{
"name": "isInlineTemplate"
},
{
"name": "isLContainer"
},
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -866,6 +866,9 @@
{
"name": "initTNodeFlags"
},
{
"name": "initializeInputAndOutputAliases"
},
{
"name": "injectArgs"
},
Expand Down
Expand Up
@@ -1013,6 +1016,9 @@
{
"name": "markAsComponentHost"
},
{
"name": "markDirtyIfOnPush"
},
{
"name": "markDuplicates"
},
Expand Down
Loading
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.