Passing Recipe Data with property binding

This commit is contained in:
francesco 2021-12-14 11:34:32 +01:00
parent 8c540dc500
commit 776ad68f87
7 changed files with 36 additions and 31 deletions

View File

@ -1,9 +1,9 @@
<app-header></app-header>
<app-header (featureSelected)="onNavgate($event)"></app-header>
<div class="container">
<div class="row">
<div class="col-md-12">
<app-recipes></app-recipes>
<app-shopping-list></app-shopping-list>
<app-recipes *ngIf="loadedFeature === 'recipe'"></app-recipes>
<app-shopping-list *ngIf="loadedFeature !== 'recipe'"></app-shopping-list>
</div>
</div>
</div>

View File

@ -7,4 +7,9 @@ import { Component } from '@angular/core';
})
export class AppComponent {
title = 'project';
loadedFeature = 'recipe';
onNavgate(feature: string) {
this.loadedFeature = feature;
}
}

View File

@ -5,8 +5,8 @@
</div>
<div class="navbar-default">
<ul class="nav navbar-nav">
<li><a href="#">Recipe</a></li>
<li><a href="#">Shopping List</a></li>
<li><a href="#" (click)="onSelect('recipe')">Recipe</a></li>
<li><a href="#" (click)="onSelect('shopping-list')">Shopping List</a></li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li class="dropdown">

View File

@ -1,15 +1,15 @@
import { Component, OnInit } from '@angular/core';
import { Component, EventEmitter, Output } from '@angular/core';
@Component({
selector: 'app-header',
templateUrl: './header.component.html',
styleUrls: ['./header.component.css']
})
export class HeaderComponent implements OnInit {
export class HeaderComponent {
@Output() featureSelected = new EventEmitter<string>();
constructor() { }
ngOnInit(): void {
onSelect(feature: string) {
this.featureSelected.emit(feature);
}
}

View File

@ -1,3 +1,15 @@
<p>
recipe-item works!
</p>
<a
href="#"
class="list-group-item clearfix">
<div class="pull-left">
<h4 class="list-group-item-heading">{{ recipe.name }}</h4>
<p class="list-group-item-text">{{ recipe.description }}</p>
</div>
<span class="pull-right">
<img
[src]="recipe.imagePath"
alt="{{ recipe.name }}"
class="img-responsive"
style="max-height: 50px;">
</span>
</a>

View File

@ -1,4 +1,5 @@
import { Component, OnInit } from '@angular/core';
import { Component, Input, OnInit } from '@angular/core';
import { Recipe } from '../../recipe.model';
@Component({
selector: 'app-recipe-item',
@ -6,6 +7,7 @@ import { Component, OnInit } from '@angular/core';
styleUrls: ['./recipe-item.component.css']
})
export class RecipeItemComponent implements OnInit {
@Input() recipe: Recipe;
constructor() { }

View File

@ -6,23 +6,9 @@
<hr>
<div class="row">
<div class="col-xs-12">
<a
href="#"
class="list-group-item clearfix"
*ngFor="let recipe of recipes">
<div class="pull-left">
<h4 class="list-group-item-heading">{{ recipe.name }}</h4>
<p class="list-group-item-text">{{ recipe.description }}</p>
</div>
<span class="pull-right">
<img
[src]="recipe.imagePath"
alt="{{ recipe.name }}"
class="img-responsive"
style="max-height: 50px;">
</span>
</a>
<app-recipe-item></app-recipe-item>
<app-recipe-item
*ngFor="let recipeElement of recipes"
[recipe]="recipeElement"></app-recipe-item>
</div>
</div>