K
K
kikosko2018-10-04 16:13:34
Angular
kikosko, 2018-10-04 16:13:34

Why doesn't ngModel see the variable?

I send the post object from the child component to the parent component, where I access the service in it, write the object to the "edData" variable, pass it to the editing component, where I assign the variable from the service to the component variable in this.editData = this.galleryService.edData;order to pass the properties of the object: "title" and " url" in ngModel for further editing of the element. But I get an error: Cannot read property 'title' of undefined"
Please help me solve this problem and understand what I'm doing wrong.
Passing an object:

editPost(pic: Picture): void {
        this.edit.emit(pic)
    }

Parent component:
editPost(pic: Picture): void {
        this.galleryService.edit(pic);
        this.collection = this.galleryService.getPictures();
    }

Service:
edData: Picture;
    edit(pic: Picture): Picture {
        return this.edData = pic;
    }

    update(title: string, url: string): void {
        const id: number = this.edData.id;
        this.Mycollection.forEach(post => {
            if (post.id === id) {
                this.edData.title = title;
                this.edData.url = url;
            }
        });
    }

TS of the editing component:
editData: Picture;

    constructor( private galleryService: GalleryService) {
        this.editData = this.galleryService.edData;
    }
    ngOnInit() {
        this.collection = this.galleryService.getPictures();
        this.editData = this.galleryService.edData;
    }
    updatePost(title: string, url: string): void {
        this.galleryService.update(title, url);
        this.collection = this.galleryService.getPictures();
    }

Edit component template:
<input type="text" class="form-control" formControlName="titleEd"  #titleEd
                       [ngModel]="editData.title"/>
                <input type="url" class="form-control" formControlName="urlEd" #urlEd pattern="https?://.+"
                       title="Include http://" [ngModel]="editData.url"/>

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question