【问题标题】:Angular 2 get dynamic value between componentsAngular 2 获取组件之间的动态值
【发布时间】:2017-10-18 03:51:21
【问题描述】:

我正在使用table pluginpagination plugin。我的目标是从分页组件中检测当前页码并将其添加到表格组件中。这怎么可能?已经试过了:

import { PaginationComponent } from '../+pagination/pagination.component';
@Component({
 ...
  providers: [PaginationComponent]
})
public currPage: number;
public constructor(private pagin: PaginationComponent) {
    this.currPage = this.pagin.page;
}

有效,但未检测到任何更改。我必须更改什么才能使其检测到更改?

【问题讨论】:

    标签: javascript twitter-bootstrap angular pagination angular2-components


    【解决方案1】:

    当你使用它时:

    <pagination (pageChanged)="pageChanged()"></pagination>
    

    在上面使用的类里面:

    export class SomethingThatUsesPagination{
        pageChanged($event){
            this.pageService.onChanged$.emit($event);
        }
    }
    

    然后创建一个服务并将其注入到两个组件中

    @Injectable()
    export class PageService{
       public onChanged$ = new EventEmitter();
    }
    

    然后在需要了解分页内容的其他组件中:

    export class OtherComponent{
        constructpr(public pageService: PageService){
            pageService.onChanged$.subscribe(($event) => {
                console.log('$event',$event);
            });
        }
    }
    

    这是两个组件通过服务进行通信。

    【讨论】:

    • 我可以设置两个参数吗?假设两个单独的字符串?我需要两个主题和两个 Observable 吗?
    猜你喜欢
    • 1970-01-01
    • 2016-07-20
    • 1970-01-01
    • 2017-05-06
    • 1970-01-01
    • 1970-01-01
    • 2017-10-25
    • 2016-11-19
    • 1970-01-01
    相关资源
    最近更新 更多