I'm facing an issue with an angular2 click event, it's not launching a simple console.log or an alert.
Here I post my component template:
<div class="col-md-4">
<div *ngFor="let phone of phones">
{{phone.text}}
</div>
<input class="form-control" type="text" [(ngModel)]="phone"/>
<button class="btn btn-primary" (click)="console.log('clicked');" type="button">Call</button>
</div>
When I click the button, there is nothing in the console.
I tried to execute a component function, with no luck as well.
I'm doing it the same way than here: http://learnangular2.com/events/
Do you guys need more files? I just don't understand why this is not working
Thank you so much, Daniel
Edit:
Ok so now I'm doing it like this:
Template:
<div class="col-md-4">
<div *ngFor="let phone of phones">
{{phone.text}}
</div>
<input class="form-control" type="text" [(ngModel)]="phone"/>
<button class="btn btn-primary" (click)="callPhone();" type="button">Call</button>
</div>
And my ts file component:
import {Component, OnInit, OnDestroy} from '@angular/core';
import {FormControl} from '@angular/forms';
import {CallService} from '../call.service';
@Component({
moduleId: module.id,
selector: 'app-call-formular',
templateUrl: './call-formular.component.html',
styleUrls: ['./call-formular.component.css'],
providers: [CallService]
})
export class CallFormularComponent implements OnInit, OnDestroy {
phones = [];
connection;
phone;
constructor(private callService: CallService) {
}
callPhone(): any {
console.log('callPhone executed.');
}
ngOnInit() {
this.connection = this.callService.getPhones().subscribe(phone =>
{
this.phones.push(phone);
});
}
ngOnDestroy() {
this.connection.unsubscribe();
}
}
And still not launching my click event
console.log
to a click event in the template? – ekoconsole
object in your component context, then only you can access it.. native browser object would not be available inside template events directly.. – Pankaj Parkar