I try to set up event (click) to button element, but I still have an error
ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'nativeElement' of undefined
TypeError: Cannot read property 'nativeElement' of undefined
When I tried to call <a (click)=EditButton(data)>Edytuj</a> there was no action, so I tried to use ChildView, Renderer, nativeElement in few version but without success.
Buttons are nested into DataTable.net Options (as column) inside Component file:
...
columns: [
{ data: "Imie" },
{ data: "Nazwisko" },
{ data: "Brygada" },
{ data: "Stawka" },
{ data: "Aktywny" },
{
data: null, defaultContent :`
<a #EditButton >Edytuj</a>
<a #DeleteButton >Usuń</a>
`
},
],
buttons: [
...
Bellow is Component code:
import { Component, OnInit, ViewChild, TemplateRef, Renderer2, AfterViewInit } from '@angular/core';
import { FadeInTop } from "../shared/animations/fade-in-top.decorator";
import { Http, Response } from '@angular/http';
import { BsModalService, ModalDirective } from 'ngx-bootstrap/modal';
import { BsModalRef } from 'ngx-bootstrap/modal/bs-modal-ref.service';
import { Observable } from "rxjs/Rx";
//import 'rxjs/add/operator/map';
//import 'rxjs/add/operator/catch';
import { bootstrapValidationRoutes } from '../+forms/+bootstrap-validation/bootstrap-validation.routing';
@FadeInTop()
@Component({
selector: 'sa-czas-pracy',
templateUrl: './czas-pracy.component.html',
styleUrls: ['./czas-pracy.component.css']
})
export class CzasPracyComponent implements OnInit {
public REST_ROOT = 'http://localhost:3000/pracownicy/pracownicy';
//private static lgModal;
options = {
dom: "Bfrtip",
ajax: (data, callback, settings) => {
this.http.get(this.REST_ROOT)
.map(this.extractData)
.catch(this.handleError)
.subscribe((data) => {
console.log('data from rest endpoint', data);
callback({
aaData: data.slice(0, 100)
})
})
},
columns: [
{ data: "Imie" },
{ data: "Nazwisko" },
{ data: "Brygada" },
{ data: "Stawka" },
{ data: "Aktywny" },
{
data: null, defaultContent :`
<button #EditButton >Edytuj</button>
<button #DeleteButton >Usuń</button>
`
},
],
buttons: [
{ text: 'Dodaj',
action: () => {this.showAddModal()}
}
],
};
@ViewChild('AddEditModal') AddEditModal :ModalDirective;
@ViewChild('EditButton') myButton;
modalRef: BsModalRef;
title: string;
constructor(
private http:Http,
private modalService: BsModalService,
private renderer: Renderer2
) {}
ngOnInit() {
// let simple1 = this.renderer.listen(this.myButton.nativeElement, 'click', (evt) => {
// console.log('Clicking the button', evt);
// });
}
ngAfterViewInit() {
let simple = this.renderer.listen(this.myButton.nativeElement, 'click', (evt) => {
console.log('Clicking the button', evt);
});
}
editPracownik(){
console.log('edit');
}
...
I searched and tried a lot of ways, but nothing work.
Any work around would be helpful.
Yes I checked and you have right. ngAfterContentChecked() called before and after DataTable.option. So I think it's ok, but myButton still is undefined.
Console when loading component:
czas-pracy.component.ts:79 AfterContentChecked
czas-pracy.component.ts:80 myButton undefined
czas-pracy.component.ts:79 AfterContentChecked
czas-pracy.component.ts:80 myButton undefined
czas-pracy.component.ts:79 AfterContentChecked
czas-pracy.component.ts:80 myButton undefined
czas-pracy.component.ts:79 AfterContentChecked
czas-pracy.component.ts:80 myButton undefined
czas-pracy.component.ts:79 AfterContentChecked
czas-pracy.component.ts:80 myButton undefined
czas-pracy.component.ts:79 AfterContentChecked
czas-pracy.component.ts:80 myButton undefined
czas-pracy.component.ts:79 AfterContentChecked
czas-pracy.component.ts:80 myButton undefined
czas-pracy.component.ts:79 AfterContentChecked
czas-pracy.component.ts:80 myButton undefined
czas-pracy.component.ts:34 data from rest endpoint (3) [{…}, {…}, {…}]
3czas-pracy.component.ts:48 Create button
czas-pracy.component.ts:48 Create button
3czas-pracy.component.ts:48 Create button
czas-pracy.component.ts:79 AfterContentChecked
czas-pracy.component.ts:80 myButton undefined
czas-pracy.component.ts:79 AfterContentChecked
czas-pracy.component.ts:80 myButton undefined
czas-pracy.component.ts:79 AfterContentChecked
czas-pracy.component.ts:80 myButton undefined
czas-pracy.component.ts:79 AfterContentChecked
czas-pracy.component.ts:80 myButton undefined
czas-pracy.component.ts:79 AfterContentChecked
czas-pracy.component.ts:80 myButton undefined
czas-pracy.component.ts:79 AfterContentChecked
czas-pracy.component.ts:80 myButton undefined
czas-pracy.component.ts:79 AfterContentChecked
czas-pracy.component.ts:80 myButton undefined
czas-pracy.component.ts:79 AfterContentChecked
czas-pracy.component.ts:80 myButton undefined
czas-pracy.component.ts:79 AfterContentChecked
czas-pracy.component.ts:80 myButton undefined
czas-pracy.component.ts:79 AfterContentChecked
czas-pracy.component.ts:80 myButton undefined
czas-pracy.component.ts:79 AfterContentChecked
czas-pracy.component.ts:80 myButton undefined
czas-pracy.component.ts:79 AfterContentChecked
czas-pracy.component.ts:80 myButton undefined
czas-pracy.component.ts:79 AfterContentChecked
czas-pracy.component.ts:80 myButton undefined
czas-pracy.component.ts:79 AfterContentChecked
czas-pracy.component.ts:80 myButton undefined
czas-pracy.component.ts:79 AfterContentChecked
czas-pracy.component.ts:80 myButton undefined
czas-pracy.component.ts:79 AfterContentChecked
czas-pracy.component.ts:80 myButton undefined
czas-pracy.component.ts:79 AfterContentChecked
czas-pracy.component.ts:80 myButton undefined
czas-pracy.component.ts:79 AfterContentChecked
czas-pracy.component.ts:80 myButton undefined
czas-pracy.component.ts:79 AfterContentChecked
czas-pracy.component.ts:80 myButton undefined
czas-pracy.component.ts:79 AfterContentChecked
czas-pracy.component.ts:80 myButton undefined
czas-pracy.component.ts:79 AfterContentChecked
czas-pracy.component.ts:80 myButton undefined
czas-pracy.component.ts:79 AfterContentChecked
czas-pracy.component.ts:80 myButton undefined
I tried refer to references by methodw witch is linked to button defined in html template - and myButton is still undefined
<a class="btn btn-danger btn-xs" (click)="findMyButton()">FindMyButton</a>
...
findMyButton(){
console.log('find my button clicked')
console.log((this.myButton) ? 'is myButton' : "myButton undefined");
if(this.myButton && !this.hasAttachedListener){
let simple = this.renderer.listen(this.myButton.nativeElement, 'click', (evt) => {
console.log('Clicking the button', evt);
});
this.hasAttachedListener=true;
}
result:
czas-pracy.component.ts:79 AfterContentChecked
czas-pracy.component.ts:80 myButton undefined
czas-pracy.component.ts:90 find my button clicked
czas-pracy.component.ts:91 myButton undefined
czas-pracy.component.ts:79 AfterContentChecked
czas-pracy.component.ts:80 myButton undefined
czas-pracy.component.ts:79 AfterContentChecked
czas-pracy.component.ts:80 myButton undefined
Changed code
export class CzasPracyComponent implements OnInit, AfterContentChecked {
public REST_ROOT = 'http://localhost:3000/pracownicy/pracownicy';
//private static lgModal;
options = {
dom: "Bfrtip",
ajax: (data, callback, settings) => {
this.http.get(this.REST_ROOT)
.map(this.extractData)
.catch(this.handleError)
.subscribe((data) => {
console.log('data from rest endpoint', data);
callback({
aaData: data.slice(0, 100)
})
})
},
columns: [
{ data: "Imie" },
{ data: "Nazwisko" },
{ data: "Brygada" },
{ data: "Stawka" },
{ data: "Aktywny" },
{
data: null, render : function (data, type, row) {
console.log('Create button');
return `
<button #EditButton >Edytuj</button>
<button #DeleteButton >Usuń</button>
`
}
},
],
buttons: [
{ text: 'Dodaj',
action: () => {this.showAddModal()}
}
],
};
@ViewChild('AddEditModal') AddEditModal :ModalDirective;
@ViewChild('EditButton') myButton;
@ViewChild('button') myButton1;
modalRef: BsModalRef;
title: string;
hasAttachedListener:boolean=false;
constructor(
private http:Http,
private modalService: BsModalService,
private renderer: Renderer2
) {}
ngOnInit() {
}
ngAfterContentChecked() {
console.log('AfterContentChecked');
console.log((this.myButton) ? 'is myButton' : "myButton undefined");
if(this.myButton && !this.hasAttachedListener){
let simple = this.renderer.listen(this.myButton.nativeElement, 'click', (evt) => {
console.log('Clicking the button', evt);
});
this.hasAttachedListener=true;
}
}
findMyButton(){
console.log('find my button clicked')
console.log((this.myButton) ? 'is myButton' : "myButton undefined");
if(this.myButton && !this.hasAttachedListener){
let simple = this.renderer.listen(this.myButton.nativeElement, 'click', (evt) => {
console.log('Clicking the button', evt);
});
this.hasAttachedListener=true;
}
}
Have you got any idea how to implement button code that be visible by angular?