簡體   English   中英

無法讀取Ionic 2中未定義的屬性“ contentHeight”

[英]Cannot read property `contentHeight` of Undefined in Ionic 2

我需要在Ionic 2頁面上獲得scrollTop位置,但是使用簡單的jQuery卻無法正常工作。

在下面查看我的工作示例,滾動頁面並單擊它,它將為您提供y位置編號。

 $("body").click(function(){ var scrollPost = $(document).scrollTop(); alert(scrollPost); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> test<br/><br/><br/><br/>kjh<br/>kjgh<br/>uitz<br/><br/><br/><br/>uztjhg<br/>kjh<br/> test<br/><br/><br/><br/>kjh<br/>kjgh<br/>uitz<br/><br/><br/><br/>uztjhg<br/>kjh<br/>test<br/><br/><br/><br/>kjh<br/>kjgh<br/>uitz<br/><br/><br/><br/>uztjhg<br/>kjh<br/>test<br/><br/><br/><br/>kjh<br/>kjgh<br/>uitz<br/><br/><br/><br/>uztjhg<br/>kjh<br/>test<br/><br/><br/><br/>kjh<br/>kjgh<br/>uitz<br/><br/><br/><br/>uztjhg<br/>kjh<br/>test<br/><br/><br/><br/>kjh<br/>kjgh<br/>uitz<br/><br/><br/><br/>uztjhg<br/>kjh<br/>test<br/><br/><br/><br/>kjh<br/>kjgh<br/>uitz<br/><br/><br/><br/>uztjhg<br/>kjh<br/>test<br/><br/><br/><br/>kjh<br/>kjgh<br/>uitz<br/><br/><br/><br/>uztjhg<br/>kjh<br/> <div class="test">Thats a test</div> 

但是獲取scrollTop的jQuery方法不起作用,它總是返回0。

所以我計划使用離子方式來獲得它, 這里的文檔

我正在嘗試使用Dimensions.contentHeightDimensions.contentTop ,但是出現錯誤。 下面是我的代碼。

home.html代碼:

<ion-content content>
    <div #topScroll>
  The world is your oyster.
  test<br/><br/><br/><br/>kjh<br/>kjgh<br/>uitz<br/><br/><br/><br/>uztjhg<br/>kjh<br/>
  test<br/><br/><br/><br/>kjh<br/>kjgh<br/>uitz<br/><br/><br/><br/>uztjhg<br/>kjh<br/>test<br/><br/><br/><br/>kjh<br/>kjgh<br/>uitz<br/><br/><br/><br/>uztjhg<br/>kjh<br/>test<br/><br/><br/><br/>kjh<br/>kjgh<br/>uitz<br/><br/><br/><br/>uztjhg<br/>kjh<br/>test<br/><br/><br/><br/>kjh<br/>kjgh<br/>uitz<br/><br/><br/><br/>uztjhg<br/>kjh<br/>test<br/><br/><br/><br/>kjh<br/>kjgh<br/>uitz<br/><br/><br/><br/>uztjhg<br/>kjh<br/>test<br/><br/><br/><br/>kjh<br/>kjgh<br/>uitz<br/><br/><br/><br/>uztjhg<br/>kjh<br/>test<br/><br/><br/><br/>kjh<br/>kjgh<br/>uitz<br/><br/><br/><br/>uztjhg<br/>kjh<br/>
  <div class="test">Thats a test</div>
</div>
</ion-content>

編輯1

home.ts代碼已更新

home.ts代碼:

    import { Content } from 'ionic-angular';
    import { Component, ViewChild } from '@angular/core';
    import { NavController } from 'ionic-angular';
    import { AfterViewInit,Renderer2, ElementRef } from '@angular/core';

    @Component({
      selector: 'page-home',
      templateUrl: 'home.html'
    })

    export class HomePage implements AfterViewInit  {

      @ViewChild(Content) content: Content;
    constructor(private renderer: Renderer2, public navCtrl: NavController) {       
    }

ngAfterViewInit() {

    // Renderer2 used not Renderer, inject it before using
    this.renderer.listen('document', 'click', (e) => {
      // Get contentHeight here
      var iscroll =  this.content.contentHeight;
// error - Property 'dimensions' does not exist on type 'Content'
      var iscroll2 = this. content.dimensions.contentHeight;
      alert(iscroll);
      alert(iscroll2);
    });
    }

出現錯誤-無法讀取未定義的屬性contentHeight

我究竟做錯了什么?

您在這里遇到多個問題:

  • 您正在使用jQuery點擊處理程序
  • 您正在使用content而不是模板中的#content

我會做:

<ion-content #content><!-- omitted content --></ion-content>

// Make sure to import AfterViewInit and implement it
ngAfterViewInit() {

  // Renderer2 used not Renderer, inject it before using
  this.renderer.listen('document', 'click', (e) => {
    // Get contentHeight here
  });
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM