Skip to content

Instantly share code, notes, and snippets.

@valorkin
Last active August 29, 2015 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save valorkin/ed9fdf26115400043eb8 to your computer and use it in GitHub Desktop.
Save valorkin/ed9fdf26115400043eb8 to your computer and use it in GitHub Desktop.
Is host element event handler is set?
// index.ts
// (close) could be set or not
@Component({
selector: 'app'
})
@View({
template: `
<alert (close)="closeAlert(i)">{{ alert1.msg }}</alert>
<alert>{{ alert2.msg }}</alert>
`,
directives: [AlertComponent, coreDirectives]
})
class Hello{}
// alerts.ts
@Component({
selector: 'alert',
properties: ['type'],
events: ['close'],
lifecycle: [LifecycleEvent.onInit]
})
@View({
template: `...doesn't care`,
directives: [coreDirectives, CSSClass]
})
class AlertComponent{
constructor(public el: ElementRef){
// TODO: is there a better way to detect that event handler is set?
this.closeable = el.nativeElement.getAttribute('(close)');
this.close = new EventEmitter();
}
}
@vsavkin
Copy link

vsavkin commented Aug 11, 2015

Currently, there is no better way to detect if a listener is set. In theory though, you could have asked the event emitter if it had a listener. This is easy to implement, but I am not sure if it is a good idea.

We have the following mental model:

  • Data flows into a component via property binding
  • Data comes out via event binding

What you are asking for somewhat conflicts with this mental model. The event binding affects the state of the component. Don't you think it might be surprising for the user?

@valorkin
Copy link
Author

@vsavkin ok another quesion :)
if I want to implement http://angular-ui.github.io/bootstrap/#/buttons in
angular2 I can not use somewhat near ng-model, and have to update model like this

  <button (on-change)="(v) => {model.field =v;}"

or there are a way to implement

  <button ([ng-model])="model.field">

?
Thank in advance :)

@valorkin
Copy link
Author

about first question I will have to use something like '[(close)]' to feat into ng2 mental model

@vsavkin
Copy link

vsavkin commented Aug 12, 2015

You can use <button [(ng-model)]="model.field">, you just need to defined a value accessor that will tell angular how to read and write a value to the button.

about first question I will have to use something like '[(close)]' to feat into ng2 mental model

[(a)] ="foo" just desugars to [a]="foo" (a)="foo=$event". I don't think it what you want. I was actually talking about the following: [show-close]="true" (close)="someHandler".

@valorkin
Copy link
Author

@vsavkin

you just need to defined a value accessor

yeap, debugged to this point yesterday,
now need to find about an interface of value accessor :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment