0

app.component.ts

viewDelete() {
 console.log('s')
 this.isDelete = !this.isDelete
 console.log(this.isDelete);
 this.viewData(id) ==>> I am getting error here id is not defined 
}

viewData(id) {
 console.log('comes')
 this.departmentService.getDepartmentById(id).subscribe(
  res => this.departmentDetail = res,
  error => this.error = error
 )
}

I want to call viewData() function from viewDelete() function. It is getting call correctly but argument id in viewData(id) giving error, id is not defined

1
  • the error is right, because where you have defined id first of all. Since it's not inside the function so let's assume you have defined at class level, so its very basic question, how to access a class-level variable. Commented May 13, 2020 at 19:23

1 Answer 1

1

You don't have any id parameter inside function. If it is general parameter write this

viewDelete() {
 console.log('s')
 this.isDelete = !this.isDelete
 console.log(this.isDelete);
 this.viewData(this.id);
}

if not and removing one of list element take id from html as parameter

viewDelete(id) {
 console.log('s')
 this.isDelete = !this.isDelete
 console.log(this.isDelete);
 this.viewData(id);
}
Sign up to request clarification or add additional context in comments.

7 Comments

This is highly dubious. What if it's a list component and there are multiple Id's? Also this is not for parameters it's for properties
Thats why I wrote second example if list it will send as parameter from html
Fair enough, but when you say "If it is general parameter write this" it doesn't make any sense
I don't know content of code . I don't know what id is. It may be general id and asking first user if delete or not so attending id to general parameter. If user say yes ,maybe delete it. That's whay i wrote two examples
So you need to send id from html to first function. If there is no id in function how can you send it to second function
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.