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="container">
<div class="row"> <div class="row">
<div class="col-md-12"> <div class="col-md-12">
<app-recipes></app-recipes> <app-recipes *ngIf="loadedFeature === 'recipe'"></app-recipes>
<app-shopping-list></app-shopping-list> <app-shopping-list *ngIf="loadedFeature !== 'recipe'"></app-shopping-list>
</div> </div>
</div> </div>
</div> </div>

View File

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

View File

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

View File

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

View File

@ -1,3 +1,15 @@
<p> <a
recipe-item works! href="#"
</p> 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({ @Component({
selector: 'app-recipe-item', selector: 'app-recipe-item',
@ -6,6 +7,7 @@ import { Component, OnInit } from '@angular/core';
styleUrls: ['./recipe-item.component.css'] styleUrls: ['./recipe-item.component.css']
}) })
export class RecipeItemComponent implements OnInit { export class RecipeItemComponent implements OnInit {
@Input() recipe: Recipe;
constructor() { } constructor() { }

View File

@ -6,23 +6,9 @@
<hr> <hr>
<div class="row"> <div class="row">
<div class="col-xs-12"> <div class="col-xs-12">
<a <app-recipe-item
href="#" *ngFor="let recipeElement of recipes"
class="list-group-item clearfix" [recipe]="recipeElement"></app-recipe-item>
*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>
</div> </div>
</div> </div>