0

person.model.ts page;

export class Person {
    name: string;
    email: string;
    desc: string;
    adres: Address;
}

export class Address {
    city: string;
    disticy: string;
    postCode: number;
    country: string;
}

person.component.html page;

...

<div class="bg-secondary container"> {{ jsonPerson }} </div>

...

<input [(ngModel)]="newPerson.adres.city" name="city" type="text" id="city" placeholder="Şehir" class="form-control">

...

form.component.ts page;

...

public newPerson: Person = new Person();

get jsonPerson() {
  return JSON.stringify(this.newPerson) 
}

...

The problem is; I want to use my address information as an object. And this is how I used it. But when I bind to my html page as [(ngModel)]="newPerson.address.city" I get an error. That is, how can I reach the Address object inside the Person object and get a successful output.

Information: person.model.ts is not my data source. I distribute my data to the necessary pages via person.service.ts. so there is no database

0

2 Answers 2

1

You need to initialize it.

export class Person {
    adres: Address = new Address();
}

or

export class Person {
    adres: Address;
    constructor() {
      this.adres = new Address();
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

teşekkür ederim
if you find this useful, please approve it by clicking on the check button near the correct answer
0

@Furkan just keep in mind that:

Support for using the ngModel input property and ngModelChange event with reactive form directives has been deprecated in Angular v6 and will be removed in a future version of Angular.

Now deprecated: <input [formControl]="control" [(ngModel)]="value">

https://angular.io/guide/deprecations#ngmodel-reactive

1 Comment

I am not very experienced. This information will be very useful for me at the learning stage. ty

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.