Angular's ControlValueAccessor has a `null` initial value causing "Cannot read property 'a' of null" ?
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.
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 :
So how come it's null ?
BTW - in native angular it doesn't happen - same code :
@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.
@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.
@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.
@RoyiNamir thanks for the update and the shared solution - I was just digging into the issue when I saw your post!
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" ("
@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.
+1
This seems to do the trick, register stack layout as form element.

