1

There are 2 uses for the my-select component:

  • the use-my-select.html template

  • created dynamically.

It works in the template but not when created dynamically.

How do I create the my-select component dynamically so that mySelected() is called?

my-select.ts

@Component({
  selector: 'my-select',
  templateUrl: './my-select.html'
})
export class MYSelect {
  @Output() mySelected: EventEmitter<string> = new EventEmitter();
}

use-my-select.html

<my-select (mySelected)="mySelected($event)" ></my-select>

use-my-selct.ts

@ViewChild('select') select: MYSelect;

Object.assign(this.select, { mySelected: "mySelected()" });

mySelected(option) {
}
4
  • I'm not sure I understand what you are trying to do. You want to swap out the event emitter? I guess I don't understand why you would do that. Can you give some more details on your situation? Commented Feb 28, 2019 at 19:42
  • The mySelected property is optional - used for special handling. When it is required, the mySelected() property needs to be added dynamically. The html template could be: <my-select></my-select> or <my-select (mySelected)="mySelected($event)" ></my-select> Commented Feb 28, 2019 at 19:59
  • It seems weird to me to have an optional output. You can always have it, then if its not needed, it just doesn't call emit. It seems extra weird to me to have the parent assign the output of a child. Outputs are for passing data from child to parent. So having a parent control the output, seems awkward. Sorry I know I'm not being helpful, but without a better understanding, Im not sure how to give you advice Commented Feb 28, 2019 at 20:13
  • The mySelected() property can be different depending on the where <my-select> is used. <my-select title="one" (mySelected)="mySelected1($event)" ></my-select> or <my-select title="two" (mySelected)="mySelected2($event)" ></my-select> Commented Feb 28, 2019 at 20:55

0

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.