0

Im currently working on an Angular App and have gotten to a problem i can't find any solution for.

I have this Schema:

const ContentSchema = new mongoose.Schema({
subject: String,
customer: String,
task: String,
date: Date,
inCharge: String,
comment: [String] })

This method in my Angular-App retrieves the Content on init of the Component.

  this.contentServ.getContent()
      .subscribe(data => this.contents  = data as mycontent[])

This is how my service is accessing the api

getContent(): Observable<mycontent[]> {
    return this.http.get<mycontent[]>('/api/retrieveContent')
  }

And this is how im displaying it

 <tr *ngFor="let content of contents">
      <th><input type="checkbox" name="contentAction" id="{{content._id}}"></th>      
      <th>{{content.subject}}</th>
      <th>{{content.customer}}</th>
      <th>{{content.task}}</th>
      <th>{{content.date | date : "dd.MM.yyyy" }}</th>
      <th>{{content.inCharge}}</th>
      <th>{{content.comment}}</th>
    </tr>

When i do this, my comments get displayed but they are displayed as they get passed by the JS from the DB:

{ comment: [ 'This is my first comment', 'i can now add another one' ],
    _id: 5bd0221100acd00f5bf8800d,
    subject: 'Application Development',
    customer: 'myCustomer',
    task: 'myTask',
    date: 2018-10-15T00:00:00.000Z,
    inCharge: 'myOfficial',
    __v: 0 }

Now the problem is, that {{content.comment}} is an array i cant access by doing this:

<tr *ngFor="let content of contents, let i = index">
      <th><input type="checkbox" name="contentAction" id="{{content._id}}"></th>      
      <th>{{content.subject}}</th>
      <th>{{content.customer}}</th>
      <th>{{content.task}}</th>
      <th>{{content.date | date : "dd.MM.yyyy" }}</th>
      <th>{{content.inCharge}}</th>
      <th>{{content.comment[i]}}</th>
    </tr>

also tried this:

<tr *ngFor="let content of contents">
      <th><input type="checkbox" name="contentAction" id="{{content._id}}"></th>      
      <th>{{content.subject}}</th>
      <th>{{content.customer}}</th>
      <th>{{content.task}}</th>
      <th>{{content.date | date : "dd.MM.yyyy" }}</th>
      <th>{{content.inCharge}}</th>
      <th><ul *ngFor="let content.comment of content.comments, let i = index><li>{{content.comment[i]}}</li></th>
    </tr>

I even tried doing this:

<th id="displayComments"></th>

and doing

 document.getElementById("displayComments").innerHTML = {{this.contents.comment}}.join(" <br> ");

But nothing of this seems to work as inteded.

My goal is to get an output in my table where i have something like

  <tr *ngFor="let content of contents">
          <th><input type="checkbox" name="contentAction" id="{{content._id}}"></th>
          <th>{{content.subject}}</th>
          <th>{{content.customer}}</th>
          <th>{{content.task}}</th>
          <th>{{content.date | date : "dd.MM.yyyy" }}</th>
          <th>{{content.inCharge}}</th>
          <th><ul>
<li>My Comment nr. 1</li>
<li>My Comment nr. 2</li>
</ul></th>
        </tr>

I hope some of you guys may be able to help me with my problem.

If you need further information of my code, I'll give it to you as fast as I can.

1
  • let content.comment of content.comments you cannot let something like that, let is used to create or ask angular to create a local variable for each iteration. It can be something like let mycomment of content.comments Commented Oct 26, 2018 at 10:23

2 Answers 2

1

First, if possible create a stackblitz or plunkr demo.

And try changing,

<th><ul *ngFor="let content.comment of content.comments, let i = index><li>{{content.comment[i]}}</li></th>
</tr>

to this,

<th><ul *ngFor="let comment of content.comments"><li>{{comment}}</li></th>
</tr>

It may solve your problem

Sign up to request clarification or add additional context in comments.

Comments

0

You could build a custom component which takes the comments array as an input and inside that component, you could use *ngFor over that array to output the markup that you want.

Comments

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.