Can ng models update objects in the model?

I am trying to write a data record with Angular 2 and Typescript, but I had a problem updating the model. As I understand it, only primitive types can be attached to ng-model . But in my model, I have objects that I want to update. Is there any Angular specific way to do this instead of loading a hole object with a modified property that binds to ng-model ?

This is the model:

export class Project {
    public id: number;
    private title: string;
    private region: Region;
}

This is the Angular Component class:

@Component({...})
export class ProjectForm {
    public project: Project;
    public regions: Array<Region>;
}

This is a view of ProjectForm:

...
<select id="region" [(ng-model)]="project.region.id">
    <option *ng-for="#region of regions" [value]="region.id">
        {{ region.name }}
    </option>
</select>
+4
source share
1 answer

Yes! Properties of objects can be associated with ngModel. See Plnkr

,

<input [(ngModel)]="project.region.id" />

<input [ngModel]="project.region.id"  (ngModelChange)="project.region.id = $event" />

,

<input [value]="project.region.id" (input)="project.region.id = $event.target.value" >

, , , ( ) .

+1

Source: https://habr.com/ru/post/1617467/


All Articles