I am trying to create components in angular 2
for this html
<div class="tab-content">
<div class="tab-pane fade in active" id="playerCard" role="tabpanel" aria-labelledby="playerCard-tab">
<div class="card">
<div class="card-block">
<h4 class="card-title text-muted">UltimateWarrior15</h4>
<h6 class="card-subtitle text-muted">
Adventurer card
</h6>
</div>
<img data-src="holder.js/100px180/?text=Image">
<div class="card-block">
<div class="form-group row">
<label class="col-xs-2 col-form-label">Rank</label>
<div class="col-xs-10">
<label class="form-control text-muted">D</label>
</div>
</div>
<div class="form-group row">
<label class="col-xs-2 col-form-label">Age</label>
<div class="col-xs-10">
<label class="form-control text-muted">15</label>
</div>
</div>
<div class="form-group row">
<label class="col-xs-2 col-form-label">Gender</label>
<div class="col-xs-10">
<label class="form-control text-muted">Male</label>
</div>
</div>
<div class="form-group row">
<label class="col-xs-2 col-form-label">Race</label>
<div class="col-xs-10">
<label class="form-control text-muted">Human</label>
</div>
</div>
</div>
</div>
</div>
</div>
I created player-record
component
import { Component } from '@angular/core';
@Component({
moduleId: module.id,
selector: 'player-record',
templateUrl: 'player-record.component.html'
})
export class PlayerRecord {
label: string;
value: string
}
and its html
template
<div class="form-group row">
<label class="col-xs-2 col-form-label">{{label}}</label>
<div class="col-xs-10">
<label class="form-control text-muted">{{value}}</label>
</div>
</div>
Then I created player-card
component
import { Component } from '@angular/core';
import { Player } from './player';
const CONTENT_CARD_META = {
"rank": "Rank",
"age": "Age",
"gender": "Gender",
"race": "Race"
};
@Component({
moduleId: module.id,
selector: 'player-card',
templateUrl: 'player-card.component.html'
})
export class PlayerCard {
player: Player;
}
Its html
template looks like this
<div class="tab-pane fade in active" id="playerCard" role="tabpanel" aria-labelledby="playerCard-tab">
<div class="card">
<div class="card-block">
<h4 class="card-title text-muted">{{player.name}}</h4>
<h6 class="card-subtitle text-muted">
Adventurer card
</h6>
</div>
<img data-src="holder.js/100px180/?text=Image">
<div class="card-block">
<!-- pseudo code
CONTENT_CARD_META.foreach { field, label ->
new PlayerRecord(label, player[field]).html
}
-->
</div>
</div>
</div>
Player
is just some kind of stub class that I am planning to replace with service call.
export class Player {
id: string;
rank: string;
age: number;
gender: string;
race: string;
}
But how to iterate over CONTENT_CARD_META
, get property from player
object by key and the add markup of player-record
component with the data inside the template of player-card
?
Sample flow I made in pseudocode
<!-- pseudo code
CONTENT_CARD_META.foreach { field, label ->
new PlayerRecord(label, player[field]).html
}
-->