nativescript-angular icon indicating copy to clipboard operation
nativescript-angular copied to clipboard

Angular's ControlValueAccessor has a `null` initial value causing "Cannot read property 'a' of null" ?

Open mid787 opened this issue 8 years ago • 8 comments

I'm using Nativescript + Angular

| nativescript     │ 3.4.3           │ 3.4.3          │ Up to date  │
│ tns-core-modules │ 3.4.1           │ 3.4.1          │ Up to date  │
│ tns-android      │ 3.4.2           │ 3.4.2          │ Up to date  │

I'm using a code which creates a custom ngModel value using the ControlValueAccessor interface .
As you can see I have 2 TextFields and a json showing the model value.

enter image description here

This is how I activate the component with value :

home.component.ts

export class HomeComponent  {

 dataModel = {
    a:1,b:2
};

...
}

home.component.html

<StackLayout >
	<app-flag  [(ngModel)]="dataModel"></app-flag>
</StackLayout>

— This is the actual component markup code :

flag.component.html

<GridLayout rows="auto,auto,auto"   columns="*" >
	    <TextField [(ngModel)]="innerValue.a" row="0" ></TextField>
		<TextField [(ngModel)]="innerValue.b" row="1"></TextField>
		<Label textWrap="true" text="{{innerValue | json}}" row=3></Label>
</GridLayout>

flag.component.ts

export class FlagComponent implements  ControlValueAccessor {
	
	innerValue: { a: 1, b: 2 }

	writeValue(value: any) {
		this.innerValue = value;
	}
...}

This code works fine , and does show the values in json. But the problem is that at t=0 , there is an error :

ERROR TypeError: Cannot read property 'a' of null

After testings - it seems that at first - the value in writeValue is null.

Question

Something is not right here. Even if there's a delay in writeValue values - it still has a public property with initial values :

enter image description here

So how come it's null ?

Playground

BTW - in native angular it doesn't happen - same code :

https://plnkr.co/edit/TyoCgvb8i8zSryxC9NXb?p=preview

mid787 avatar Apr 02 '18 16:04 mid787

@NickIliev Hi :) it's not a question , it's a bug. In native angular it doesn't happen. I've described a non-matching-to angular behaviour. As you can see here - it doesn't happen : https://plnkr.co/edit/TyoCgvb8i8zSryxC9NXb?p=preview - same code.

mid787 avatar Apr 03 '18 06:04 mid787

@NickIliev Can you revisit this thread ? from all my testings - Nativescript ControlValueAccessor class is not behaving like the expected ! it won't initialize fields ! . I may have cause it to be sounded as a question - but it is not.

mid787 avatar Apr 03 '18 18:04 mid787

@NickIliev update - it is a bug in angular : https://github.com/angular/angular/issues/14988. And it also happens in web if you don't have FORM tag. : Example : https://plnkr.co/edit/2MaEsD7Ru5aGu6VH6Z5U?p=preview

But in Ns we don't have form tag - and that's why it happens here.

One Solution is to wrap with Stacklayout which has ngForm with name

Just updating.

mid787 avatar Apr 03 '18 18:04 mid787

@RoyiNamir thanks for the update and the shared solution - I was just digging into the issue when I saw your post!

NickIliev avatar Apr 04 '18 06:04 NickIliev

So i want to use ngForm on my forms to verify if they are valid with myForm.form.valid and seems like it is not possible with StackLayout's. Can someone show a sample to do that?

When i try: <StackLayout #myForm="ngForm"> Appears an error on console: There is no directive with "exportAs" set to "ngForm" ("

yassernasc avatar Nov 06 '18 14:11 yassernasc

@RoyiNamir Can you please show a example of how the StackLayout is supposed to be and if i am doing right on my comment above? thanks.

yassernasc avatar Dec 05 '18 11:12 yassernasc

+1

jpnobrega avatar Feb 13 '20 22:02 jpnobrega

This seems to do the trick, register stack layout as form element.

manojdcoder avatar Apr 09 '20 21:04 manojdcoder