Categories
Angular Answers

How to call a method in the child component with Angular?

Sometimes, we want to call a method in the child component with Angular.

In this article, we’ll look at how to call a method in the child component with Angular.

How to call a method in the child component with Angular?

To call a method in the child component with Angular, we can use ViewChild.

For instance, we write

@Component({
  // ...
})
export class Parent {
  @ViewChild(Child) child: Child;

  incrementSonBy5() {
    this.child.updateData(this.child.data + 5);
  }
}

to create the Parent component.

We reference the Child component with @ViewChild(Child) child: Child;.

Then we get the properties and methods in Child with this.child.

Next, we add the Child component with

@Component({
  // ...
  template: `{{ data }}`,
  // ...
})
export class Child {
  data: number = 3;

  constructor() {}

  updateData(data: number) {
    this.data = data;
  }
}

We add the data property and the updateData component method that were referenced in Parent.

Conclusion

To call a method in the child component with Angular, we can use ViewChild.

Categories
JavaScript Answers

How to remove a child node in HTML using JavaScript?

Sometimes, we want to remove a child node in HTML using JavaScript.

In this article, we’ll look at how to remove a child node in HTML using JavaScript.

How to remove a child node in HTML using JavaScript?

To remove a child node in HTML using JavaScript, we can use the remove method.

For instance, we write

document.getElementById("FirstDiv").remove();

to select the element with getElementById.

Then we call remove to remove the selected element.

Conclusion

To remove a child node in HTML using JavaScript, we can use the remove method.

Categories
JavaScript Answers

How to check whether a variable is defined in Node.js and JavaScript?

Sometimes, we want to check whether a variable is defined in Node.js and JavaScript.

In this article, we’ll look at how to check whether a variable is defined in Node.js and JavaScript.

How to check whether a variable is defined in Node.js and JavaScript?

To check whether a variable is defined in Node.js and JavaScript, we can check if it’s not null or undefined.

For instance, we write

if (typeof query !== "undefined" && query !== null) {
  doStuff();
}

to check if query isn’t undefined with typeof query !== "undefined".

And we check if query isn’t null with query !== null.

Conclusion

To check whether a variable is defined in Node.js and JavaScript, we can check if it’s not null or undefined.

Categories
JavaScript Answers

How to find all indexes of a specified character within a string with JavaScript?

Sometimes, we want to find all indexes of a specified character within a string with JavaScript.

In this article, we’ll look at how to find all indexes of a specified character within a string with JavaScript.

How to find all indexes of a specified character within a string with JavaScript?

To find all indexes of a specified character within a string with JavaScript, we can use the string matchAll method.

For instance, we write

const string = "scissors";
const matches = [...string.matchAll(/s/g)];
const indexes = matches.map((match) => match.index);

to call string.matchAll with a regex to find all instances of 's' in string.

Then we call matches.map with a callback that gets the index property of each match object to get the index of each match in a new array and return it.

Conclusion

To find all indexes of a specified character within a string with JavaScript, we can use the string matchAll method.

Categories
JavaScript Answers

How to delete document from Firestore using where clause with JavaScript?

Sometimes, we want to delete document from Firestore using where clause with JavaScript.

In this article, we’ll look at how to delete document from Firestore using where clause with JavaScript.

How to delete document from Firestore using where clause with JavaScript?

To delete document from Firestore using where clause with JavaScript, we can use the delete method.

For instance, we write

const jobSkills = await store
  .collection("job_skills")
  .where("job_id", "==", post.job_id)
  .get();

const batch = store.batch();

jobSkills.forEach((doc) => {
  batch.delete(doc.ref);
});

await batch.commit();

to get the entries we want to delete with where and get.

We call batch to catch the delete operations.

Then we call forEach with a callback that calls batch.delete to delete the entry by referencing it with doc.ref.

Next we call batch.commit to commit the delete operations.

Conclusion

To delete document from Firestore using where clause with JavaScript, we can use the delete method.