0

Just having a bit of difficulty passing a router param (id) to an ngForm and then to an event emitter. Here is how I get the id - as you can see the first console.log works fine, i can get the id from the router. Now I am trying to assign this to my singleOpenHome object (but get an undefined error):

@Input() singleOpenHome: OpenHomeModel;

ngOnInit() {
        // Get the slug on it from the router
        this.route.params.forEach((params: Params) => {
            let id = params['propertyID'];
            console.log('id on the child is:'+id);
            //this.singleOpenHome.propertyID == id; Cannot read property 'propertyID' of undefined
        });
    }

Now i am wanting to pass this to a form:

 <form class="my2" #form="ngForm" (ngSubmit)="handleSubmit(form.value, form.valid)" novalidate></form>

Here is my event emmitter:

@Output()
    update: EventEmitter<OpenHomeModel> = new EventEmitter<OpenHomeModel>();

    constructor(private route: ActivatedRoute) {}

    handleSubmit(sinlgeOpenHome: OpenHomeModel, isValid: boolean) {
        if (isValid) {
            this.update.emit(sinlgeOpenHome);
        }
    }

Any help appreciated

7
  • Does all the code you showed belong to the same component? If yes, just assign the route param to a class property — this.id = params['propertyID']; — then you can access the property from anywhere in the component or its template. Commented Feb 17, 2017 at 10:38
  • Tried this: this.singleOpenHome.propertyID = params['propertyID']; But i cant show the value in the template like {{ singleOpenHome.propertyID }} wont work. Commented Feb 17, 2017 at 10:46
  • I don't understand. Why don't you try the code I suggested? Does all the code you showed belong to the same component? Commented Feb 17, 2017 at 10:49
  • Yes all the same component. Where should I put the this.id... inside my ngOnInit? Commented Feb 17, 2017 at 10:51
  • 1
    Yes. let id = ... becomes this.id = .... You'll also need to declare the id property on the class. Commented Feb 17, 2017 at 10:55

1 Answer 1

1

I've experienced problems specifically related to converting the route.params Observable into a Promise which is what forEach does. Consider simply using subscribe

ngOnInit() {
    // Get the slug on it from the router
  this.route.params.subscribe(params => {
    const id = params['propertyID'];
    console.log(`id on the child is: ${id}`);
  });
}

This looks line like a typo

this.singleOpenHome.propertyID == id;

You're performing a comparison instead of an assignment.

That said you may have other bugs which lead to the property being undefined. Since it's marked as an input I assume it comes from an external binding. Make sure the value that you are passing into this binding is properly initialized.

Sign up to request clarification or add additional context in comments.

1 Comment

You are so right - I haven't done my bindings correctly, I have a parent container which should initialize the singleOpenHome object and pass it into it's child component, then I can assign the propertyID variable (but I will pass it in with binding from the parent)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.