g) Show messages - Display the message from HeroService + Bind to the
MessageService
+/* MessagesComponent's private CSS styles */
+h2 {
+ color: red;
+ font-family: Arial, Helvetica, sans-serif;
+ font-weight: lighter;
+}
+body {
+ margin: 2em;
+}
+body, input[text], button {
+ color: crimson;
+ font-family: Cambria, Georgia;
+}
+
+button.clear {
+ font-family: Arial;
+ background-color: #eee;
+ border: none;
+ padding: 5px 10px;
+ border-radius: 4px;
+ cursor: pointer;
+ cursor: hand;
+}
+button:hover {
+ background-color: #cfd8dc;
+}
+button:disabled {
+ background-color: #eee;
+ color: #aaa;
+ cursor: auto;
+}
+button.clear {
+ color: #333;
+ margin-bottom: 12px;
+}
-<p>messages works!</p>
+<div *ngIf="messageService.messages.length">
+ <h2>Messages</h2>
+ <button class="clear" (click)="messageService.clear()">clear</button>
+ <div *ngFor="let message of messageService.messages">{{message}}</div>
+</div>
import { Component, OnInit } from '@angular/core';
+import { MessageService } from '../message.service';
@Component({
selector: 'app-messages',
})
export class MessagesComponent implements OnInit {
- constructor() { }
+ constructor(public messageService : MessageService) { }
ngOnInit() {
}