首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >角4兄弟通信

角4兄弟通信
EN

Stack Overflow用户
提问于 2017-09-11 05:39:12
回答 1查看 482关注 0票数 0

与服务的同级组件通信(模拟redux fn)

  • 在仪表板中添加任务日期和数量
  • 更新仪表板cmp中的任务日期和数量
  • 从仪表板cmp空出英雄- todos cmp中的所有按钮

这是可行的,优雅而简单的使用角度吗?

代码语言:javascript
复制
//our root app component
    import {Component, NgModule, Input, Output, EventEmitter, VERSION} from '@angular/core'
    import {BrowserModule} from '@angular/platform-browser'
    @Component({
      selector: 'my-app',
      template: `
    <div>
      <h2>{{name}}</h2>
      <app-heroes-todos></app-heroes-todos>
      <app-heroes-dashboard></app-heroes-dashboard>
    </div>
  `,
})
export class App {
  name:string;
  constructor() {
    this.name = `We Honor Angular! v${VERSION.full}`;
  }
}

interface Todos {
  title: string;
  isCompleted: boolean;
  date: number;
}

@Component({
  selector: 'app-heroes-todos',
  styles: [`
    .completed {
      text-decoration: line-through;
    }
  `],
  template: `
  <h2>Mission List</h2>
<input #input>
<button (click)="add(input)">Add</button>
<ul>
  <li *ngFor="let todo of todos">
    <span [class.completed]="todo.isCompleted" (click)="toggle(todo)" >{{todo.title}}</span>
    <button (click)="remove(todo)">X</button>
  </li>
</ul>`
})
export class HeroesTodos {

todos: Todos[] = [];
  totalItems = 0;
  lastUpdate: any;

  add(input) {
    this.todos.push({title: input.value, isCompleted: false, date: new Date().getTime()});
    this.lastUpdate = new Date().getTime();
    this.totalItems++;
    input.value = '';
  }

  remove(todo) {
    let index = this.todos.indexOf(todo);
    this.lastUpdate = new Date().getTime();
    this.totalItems--;
    this.todos.splice(index, 1);
  }
  toggle(todo) {
    todo.isCompleted = !todo.isCompleted;
  }

  deleteAll(clearData: boolean) {
    if (clearData) {
    this.todos = [];
    this.totalItems = 0;
    this.lastUpdate = new Date().getTime();
  }
  }
}
@Component({
  selector: 'app-heroes-dashboard',
  template: `
  <div>
        <h2>Dashboard</h2>
        <p><b>Last Update:</b>{{ lastUpdate | date:'medium' }}</p>
        <p><b>Total Missions:</b> {{ totalItems }}</p>
        <button (click)="deleteAll()">Delete All</button>
</div>
  `
})
export class HeroesDashboard {
  @Input() totalItems;
  @Input() lastUpdate;
  @Output() onDeleted = new EventEmitter<boolean>();

  constructor() { }

  deleteAll() {
    this.onDeleted.emit(true);
  }
}

@NgModule({
  imports: [ BrowserModule ],
  declarations: [ App, HeroesTodos, HeroesDashboard ],
  bootstrap: [ App ]
})
export class AppModule {}
EN

回答 1

Stack Overflow用户

发布于 2017-09-11 19:10:47

代码语言:javascript
复制
import { Injectable } from '@angular/core';
import { Subject } from 'rxjs/Subject';

@Injectable()
export class SharedService {
  //observable string source
  private totalCount = new Subject<number>();
  private lastUpdate = new Subject<number>();
  private clearAll = new Subject<boolean>();

  // observable string streams
  totalCount$ = this.totalCount.asObservable();
  lastUpdate$ = this.lastUpdate.asObservable();
  clearAll$ = this.clearAll.asObservable();

  // service message commands
  publishTotalCount(count: number) {
    this.totalCount.next(count);
  }

  publishLastUpdate(date: number) {
    this.lastUpdate.next(date);
  }

  publishClearAll(clear: boolean) {
    this.clearAll.next(clear);
  }

}

https://plnkr.co/edit/UEPbIj4OmfWrMuU9jpzN?p=preview

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46148851

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档