-
-
Save valorkin/ed9fdf26115400043eb8 to your computer and use it in GitHub Desktop.
// 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 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 :)
about first question I will have to use something like '[(close)]' to feat into ng2 mental model
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"
.
you just need to defined a value accessor
yeap, debugged to this point yesterday,
now need to find about an interface of value accessor :)
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:
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?