Hi I would know how can I use variables of the component in nested function.
Here's an example:
export class AppComponent implements OnInit {
name = ['Angular 6','Angular5','Angular4','Angular2'];
isexist: string[]=[];
ifExist(text){
var that= this;
console.log("first ",this);
var test="test";
let exist= this.name.map(function (elm){
if(elm==text) {
console.log(this);
this.isexist.push(elm); // works with that.isexist.push(elm);
}
})
}
ngOnInit() {
this.ifExist('Angular 6');
}
Here's what I get in browser dev tool
first AppComponent {name: Array(4), namev: "helo", isexist: Array(1)};
second undefined
I have some questions
How can I access to isexist
without using arrow funtion ?
why the second this
does not contain test
element ?