NAiVE_developer
NAiVE_developer

Reputation: 11

Angular pipe not getting applied inside foreach loop

I am trying to use a pipe inside a foreach loop in my angular component. My array is populated and so is each item of the array when I debug it

   console.log(this.blog);
    this.relatedLinks = this.blog.related;
     this.relatedLinks.forEach(function (link) {
        this.newlink = this.removeSpace.transform(link);
        this.relatedBlogList = this.Blogs.filter(blog => blog.title === this.newlink);
        });
  },

But while applying the pipe I get the following error: ERROR TypeError: Cannot read property 'removeSpace' of undefined.

Upvotes: 0

Views: 2464

Answers (1)

Neeraj
Neeraj

Reputation: 483

I think this is not accessible inside the foreach. Try this and post back with progress.

console.log(this.blog);
    this.relatedLinks = this.blog.related;
    let self = this;
     this.relatedLinks.forEach(function (link) {
        this.newlink = self.removeSpace.transform(link);
        this.relatedBlogList = this.Blogs.filter(blog => blog.title === this.newlink);
        });
  },

Upvotes: 1

Related Questions