0

I have a use case in AngularJS 2 where I have an object with an attribute that is an array of keys, like this:

export class Region {
    m49: string;
    parent: string;
    regions: string[];
    countries: string[];
    name: string; 
}

I have a service that takes a key, and returns the corresponding object:

@Injectable()
export class PlaceService {
    getRegion(m49: string) {
        ...
    }
    getCountry(iso: string) {
        ...
    }
}

I'm trying to build a component that displays the information about a region, and then lists the sub-regions and countries. This displays the region and country keys properly.

@Component({
    selector: 'places',
    template: `
<div>
  <h2>{{region.name}}</h2>
  <div>
    <h3>Regions</h3>
    <ul>
      <li *ngFor="let m49 of region.regions">{{m49}}</li>
    </ul>
  </div>
  <div>
    <h3>Countries</h3>
    <ul>
      <li *ngFor="let iso of region.countries">{{iso}}</li>
    </ul>
  </div>
</div>
`,
    providers: [ PlaceService ]
})

But what I'd really like to do is to get the region and country objects in the loops, and display their properties. How do I invoke a method of the component class to call the service, and retrieve an object, from the <li> elements? Secondarily, how can I use *ngIf on the two child div elements to only include them if the respective lists are non-empty?

2 Answers 2

1

to call the component method do this

{{ componentMethod() }}

and to include div if list is not empty do this

<div *ngIf="list.length > 0"></div>
Sign up to request clarification or add additional context in comments.

8 Comments

Thanks for the quick answer. If componentMethod() returns an object (say, of the Region type above), how would I assign the return value to a local variable so that I can use it within the HTML element?
you can return the specific property or you can use json pipe like this to display whole object <div>{{componentMethodObject | json}}</div>
That's closer, but how would I, for example, display the m49 and name attribute of a region object in the list? I know the following isn't correct, but something like: <..."with r = getRegion()">{{r.m49}}: {{r.name}}
for this you have to do like this {{getRegion().m49}} : {{getRegion().name}}
Okay, thanks. It looks like, for this particular use case, the best way is to make another sub-component with just the <li> element.
|
0
type UserData = {
  name: string;
  count: number;
  status: 'recived' | 'notrecived';
};

type SummarizedData = {
  name: string;
  recivedCount: number;
  notrecivedCount: number;
};

const data: UserData[] = [
  { name: 'user1', count: 4, status: 'recived' },
  { name: 'user2', count: 2, status: 'recived' },
  { name: 'user1', count: 1, status: 'notrecived' },
  { name: 'user2', count: 5, status: 'notrecived' },
];

// Transform the data to group by user and summarize the counts
const summarizeData = (data: UserData[]): SummarizedData[] => {
  const result: { [key: string]: { recivedCount: number; notrecivedCount: number } } = {};

  data.forEach(item => {
    if (!result[item.name]) {
      result[item.name] = { recivedCount: 0, notrecivedCount: 0 };
    }
    if (item.status === 'recived') {
      result[item.name].recivedCount += item.count;
    } else {
      result[item.name].notrecivedCount += item.count;
    }
  });

  // Convert the result object into an array
  return Object.keys(result).map(name => ({
    name,
    recivedCount: result[name].recivedCount,
    notrecivedCount: result[name].notrecivedCount,
  }));
};

// Example usage
const summarized = summarizeData(data);
console.log(summarized);

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.