Skip to content

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

@RoyiNamir

Description

@RoyiNamir

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions