我需要在另一个产品组件的app.module.ts中滚动标记
app.module.ts
<div class="mdl-layout mdl-js-layout mdl-layout--fixed-header">
<nav-header></nav-header>
<main class="mdl-layout__content" #scrollMe>
<router-outlet></router-outlet>
<app-footer [otherType]="'siteSetting'" [type]="'getAllPages'"></app-footer>
</main>
</div>
<cart-quantity-icon></cart-quantity-icon>
主标记,即#scrollMe需要使用以下组件滚动
products.components.ts
import { Component, OnInit,ViewChild,ElementRef,ContentChild } from '@angular/core';
import { ActivatedRoute, NavigationEnd ,Router, Event, NavigationStart, RoutesRecognized,RouteConfigLoadStart,
RouteConfigLoadEnd, NavigationCancel, NavigationError} from '@angular/router';
@Component({
selector: 'app-products',
templateUrl: './products.component.html',
styleUrls: ['./products.component.css']
})
export class ProductsComponent implements OnInit {
scrollTopButton = false;
@ViewChild('infinite') private myScrollContainer : ElementRef;
@ContentChild('scrollMe') private myScroll : ElementRef;
scrollToTop() {
console.log(this.scrollTopButton);
this.myScrollContainer.nativeElement.scrollTo(0, 0);
this.myScroll.nativeElement.scrollTo(0, 0);
window.scrollTo(0,0);
//this.content.scrollToTop(700);
this.scrollTopButton = false;
}}
<div style=" max-height: 750px;
overflow: auto;" #infinite infiniteScroll
[infiniteScrollDistance]="2"
[infiniteScrollThrottle]="1000"
(scrolled)="getProducts()" [scrollWindow]="false" class="mdl-grid mdl-cell mdl-cell--top mdl-cell--9-col mdl-cell--8-col-tablet mdl-cell--4-col-phone">
<!-- new card -->
<product *ngFor="let p of products" [data]="p" [type]="'normal'"></product>
<div >
</div>
<button *ngIf="scrollTopButton" (click)="scrollToTop()" id="return-to-top" mat-fab color="primary"><i class="material-icons" style="font-size: 24px;">keyboard_arrow_up</i></button>
当我点击返回顶部按钮时,我需要滚动#infinite和#rollMe。我试过跟随。而不是contentChild尝试了viewChild
@ViewChild('infinite') private myScrollContainer : ElementRef;
@ContentChild('scrollMe') private myScroll : ElementRef; //Also ViewChild here but none works!!
scrollToTop() {
console.log(this.scrollTopButton);
this.myScrollContainer.nativeElement.scrollTo(0, 0);
this.myScroll.nativeElement.scrollTo(0, 0);
window.scrollTo(0,0);
//this.content.scrollToTop(700);
this.scrollTopButton = false;
}}
答案 0 :(得分:1)
这不起作用,因为来自app-products的ContentChild无法访问DOM中的父元素。您必须使用服务进行通信。 ComService创建和提供。
import { Injectable, EventEmitter } from '@angular/core';
import { Observable, Subject } from 'rxjs';
@Injectable()
export class ComService {
messageStream = new Subject();
getMessageSubscribe() {
return Observable.from(this.messageStream);
}
sendMessage() {
this.messageStream.next(this.filteredOrderList);
}
}
在组件主要组件订阅消息中:
@ViewChild('scrollMe') private myScroll : ElementRef;
constructor(
private comService: ComService
) {
this.subscribeMes = this.comService.getComunication().subsribe(element => {
if(element) {
this.myScroll.nativeElement.scrollTo(0, 0);
//when element is true you got info from child to scroll
}
})
ngOnDestroy() {
this.subscribeMes.unsubscribe();
}
}
#scrollMe插入主要组件模板的顶部元素。 在应用程序产品中:
constructor(
private comService: ComService
) {}
@ViewChild('infinite') private myScrollContainer : ElementRef;
scrollToTop() {
console.log(this.scrollTopButton);
this.myScrollContainer.nativeElement.scrollTo(0, 0);
this.comService.sendMessage(true);
window.scrollTo(0,0);
//this.content.scrollToTop(700);
this.scrollTopButton = false;
}}