I have an items array and loop it into a component. Display items props separately:
Items Array
const items = [
{
'name': 'item1',
'prop1': 'text1',
'prop2': 'text2',
'boolval': true
},
{
'name': 'item2',
'prop1': 'text1',
'prop2': 'text2',
'boolval': false
},
{
'name': 'item3',
'prop1': 'text1',
'prop2': 'text2',
'boolval': true
}];
Component
<div class="all-items">
<app-items [item]="items"></app-items>
</div>
<div class="specific-items">
<app-items [item]="items"></app-items>
</div>
HTML
<div class="title">List</div>
<p>Name: {{item.name}}</p>
<p>Prop1: {{item.prop1}}</p>
<p>Prop2: {{item.prop2}}</p>
I want to display all items under the .all-items div, but display item which has boolval=true prop under the .specific-items div. Generated HTML for all-items looks like the following:
<div class="all-items">
<app-items>
<div class="title">List</div>
<p>Name: item1</p>
<p>Prop1: text1</p>
<p>Prop2: text2</p>
</app-items>
<app-items>
<div class="title">List</div>
<p>Name: item2</p>
<p>Prop1: text1</p>
<p>Prop2: text2</p>
</app-items>
<app-items>
<div class="title">List</div>
<p>Name: item3</p>
<p>Prop1: text1</p>
<p>Prop2: text2</p>
</app-items>
</div>
I want to display specific items like this;
<div class="specific-items">
<app-items [item]="items.filter(x => x.boolval=== true)"></app-items>
</div>
<div class="specific-items">
<app-items>
<div class="title">List</div>
<p>Name: item1</p>
<p>Prop1: text1</p>
<p>Prop2: text2</p>
<p>Name: item3</p>
<p>Prop1: text1</p>
<p>Prop2: text2</p>
</app-items>
</div>
How can I generate a specific array in template like this [item]="items.filter(x => x.boolval=== true)" and pass it to component? So I want generate a specific array in template and then send it to component
.map(x => x.bool === true)to do? This will return a new array of booleans You probably wanted .filter instead (you should use aPipefor that though angular.io/guide/pipes) . That said, you should either use a component getter, either generate the array first, it's not a great practice to bind a function (or a getter) to an angular component if it performs heavy operations (like array mapping).x.bool? Your array items have a field calledboolval.