77 */
88import { RuntimeError , RuntimeErrorCode } from '../../errors' ;
99import { getClosureSafeProperty } from '../../util/property' ;
10+ import { assertFirstCreatePass } from '../assert' ;
1011import { bindingUpdated } from '../bindings' ;
1112import { ɵCONTROL , ɵControl , ɵFieldState } from '../interfaces/control' ;
1213import { ComponentDef } from '../interfaces/definition' ;
@@ -47,9 +48,12 @@ export function ɵɵcontrolCreate(): void {
4748 const lView = getLView < { } | null > ( ) ;
4849 const tView = getTView ( ) ;
4950 const tNode = getCurrentTNode ( ) ! ;
50- const control = tView . firstCreatePass
51- ? getControlDirectiveFirstCreatePass ( tView , tNode , lView )
52- : getControlDirective ( tNode , lView ) ;
51+
52+ if ( tView . firstCreatePass ) {
53+ initializeControlFirstCreatePass ( tView , tNode , lView ) ;
54+ }
55+
56+ const control = getControlDirective ( tNode , lView ) ;
5357
5458 if ( ! control ) {
5559 return ;
@@ -114,11 +118,9 @@ export function ɵɵcontrol<T>(value: T, sanitizer?: SanitizerFn | null): void {
114118const HAS_CONTROL_MASK = /* @__PURE__ */ ( ( ) =>
115119 TNodeFlags . isNativeControl | TNodeFlags . isFormValueControl | TNodeFlags . isFormCheckboxControl ) ( ) ;
116120
117- function getControlDirectiveFirstCreatePass < T > (
118- tView : TView ,
119- tNode : TNode ,
120- lView : LView ,
121- ) : ɵControl < T > | undefined {
121+ function initializeControlFirstCreatePass < T > ( tView : TView , tNode : TNode , lView : LView ) : void {
122+ ngDevMode && assertFirstCreatePass ( tView ) ;
123+
122124 const directiveIndices = tNode . inputs ?. [ 'field' ] ;
123125 if ( ! directiveIndices ) {
124126 // There are no matching inputs for the `[field]` property binding.
@@ -137,13 +139,22 @@ function getControlDirectiveFirstCreatePass<T>(
137139 }
138140
139141 // Search for the `ɵControl` directive.
140- const control = findControlDirective < T > ( lView , directiveIndices ) ;
141- if ( ! control ) {
142+ let controlIndex = - 1 ;
143+
144+ for ( let index of directiveIndices ) {
145+ if ( ɵCONTROL in lView [ index ] ) {
146+ controlIndex = index ;
147+ break ;
148+ }
149+ }
150+
151+ if ( controlIndex === - 1 ) {
142152 // The `ɵControl` directive was not imported by this component.
143153 return ;
144154 }
145155
146- tNode . flags |= TNodeFlags . isFormControl ;
156+ const control = lView [ controlIndex ] as ɵControl < T > ;
157+ tNode . fieldIndex = controlIndex ;
147158
148159 if ( isComponentHost ( tNode ) ) {
149160 const componentDef = tView . data [ componentIndex ] as ComponentDef < unknown > ;
@@ -158,7 +169,7 @@ function getControlDirectiveFirstCreatePass<T>(
158169 // Only check for an interop control if we haven't already found a custom one.
159170 if ( ! ( tNode . flags & HAS_CONTROL_MASK ) && control . ɵinteropControl ) {
160171 tNode . flags |= TNodeFlags . isInteropControl ;
161- return control ;
172+ return ;
162173 }
163174
164175 if ( isNativeControl ( tNode ) ) {
@@ -172,7 +183,7 @@ function getControlDirectiveFirstCreatePass<T>(
172183 }
173184
174185 if ( tNode . flags & HAS_CONTROL_MASK ) {
175- return control ;
186+ return ;
176187 }
177188
178189 const tagName = tNode . value ;
@@ -193,25 +204,9 @@ function getControlDirectiveFirstCreatePass<T>(
193204 * @param tNode The `TNode` of the element to check.
194205 * @param lView The `LView` that contains the element.
195206 */
196- function getControlDirective < T > ( tNode : TNode , lView : LView ) : ɵControl < T > | undefined {
197- return tNode . flags & TNodeFlags . isFormControl
198- ? findControlDirective ( lView , tNode . inputs ! [ 'field' ] )
199- : undefined ;
200- }
201-
202- function findControlDirective < T > (
203- lView : LView ,
204- directiveIndices : number [ ] ,
205- ) : ɵControl < T > | undefined {
206- for ( let index of directiveIndices ) {
207- const directive = lView [ index ] ;
208- if ( ɵCONTROL in directive ) {
209- return directive ;
210- }
211- }
212-
213- // The `Field` directive was not imported by this component.
214- return ;
207+ function getControlDirective < T > ( tNode : TNode , lView : LView ) : ɵControl < T > | null {
208+ const index = tNode . fieldIndex ;
209+ return index === - 1 ? null : lView [ index ] ;
215210}
216211
217212/** Returns whether the specified `componentDef` has a model input named `name`. */
0 commit comments