GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

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.

Sign up for GitHub

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

Closed
pkozlowski-opensource wants to merge 1 commit into angular:main from pkozlowski-opensource:fixit_set_input

Uh oh!

There was an error while loading. Please reload this page.

Closed

feat(core): add ability to set inputs on ComponentRef #46641

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
1 commit
Select commit
982f88a
feat(core): add ability to set inputs on ComponentRef
pkozlowski-opensource Jun 30, 2022
File filter
Filter by extension

Clear filters
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
Diff view

There are no files selected for viewing

1 change: 1 addition & 0 deletions 1 goldens/public-api/core/index.md
Show comments
View file
Open in desktop
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
4 changes: 2 additions & 2 deletions 4 goldens/size-tracking/integration-payloads.json
Show comments
View file
Open in desktop
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
}
}
}
}
10 changes: 10 additions & 0 deletions 10 packages/core/src/linker/component_factory.ts
Show comments
View file
Open in desktop
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
30 changes: 25 additions & 5 deletions 30 packages/core/src/render3/component_ref.ts
Show comments
View file
Open in desktop
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
4 changes: 4 additions & 0 deletions 4 packages/core/src/render3/instructions/element_validation.ts
Show comments
View file
Open in desktop
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
5 changes: 3 additions & 2 deletions 5 packages/core/src/render3/instructions/shared.ts
Show comments
View file
Open in desktop
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
6 changes: 6 additions & 0 deletions 6 packages/core/test/bundling/animations/bundle.golden_symbols.json
Show comments
View file
Open in desktop
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
6 changes: 6 additions & 0 deletions 6 packages/core/test/bundling/cyclic_import/bundle.golden_symbols.json
Show comments
View file
Open in desktop
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
6 changes: 6 additions & 0 deletions 6 packages/core/test/bundling/forms_reactive/bundle.golden_symbols.json
Show comments
View file
Open in desktop
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
6 changes: 6 additions & 0 deletions 6 packages/core/test/bundling/forms_template_driven/bundle.golden_symbols.json
Show comments
View file
Open in desktop
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
9 changes: 9 additions & 0 deletions 9 packages/core/test/bundling/hello_world/bundle.golden_symbols.json
Show comments
View file
Open in desktop
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
6 changes: 6 additions & 0 deletions 6 packages/core/test/bundling/router/bundle.golden_symbols.json
Show comments
View file
Open in desktop
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
9 changes: 9 additions & 0 deletions 9 packages/core/test/bundling/standalone_bootstrap/bundle.golden_symbols.json
Show comments
View file
Open in desktop
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
6 changes: 6 additions & 0 deletions 6 packages/core/test/bundling/todo/bundle.golden_symbols.json
Show comments
View file
Open in desktop
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.
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.