2016-12-09 11 views
3

ElementRefオブジェクトをAngular2(Ionic2)で調べており、ElementRefpadding CSS属性を保持できません。angular2:ElementRef CSSパディング属性を取得

次の質問については、[my project] \ src \ app \ app.component.tsと[my project] \ src \ app \ app.module.tsの詳細については説明しません。これらの作業を正しく行うようにしてください。

import { Component, ContentChild, ViewChild, ElementRef } from '@angular/core'; 
import { NavController, Card, CardContent } from 'ionic-angular'; 

@Component({ 
    selector:'my-component', 
    templateUrl: 'my-component.html' 
}) 
export class MyComponentPage { 
    // @ContentChild("ionCard") ionCard:Card; 
    // @ContentChild("ionCardContent") ionCardContent:CardContent; 
    @ViewChild("ionCard") ionCard:ElementRef; 
    @ViewChild("ionCardContent") ionCardContent:ElementRef; 
    constructor(public navCtrl:NavController){ 

    } 
    ionViewWillEnter(){ 
     console.log("ionCard: ", ionCard); 
     console.log("ionCardContent: ", ionCardContent); 

    } 
} 

その後でこのHTMLテンプレート[私のプロジェクト] \ SRC \:

私は[私のプロジェクト] \ SRC \ページは私の-component.ts \私の成分を\の下のコンポーネントでこのコードを持っていますページは私の-component.html \私の成分を\、私が持っている:私は#ionCard#ionCardContentため(ionWillEnter()console.logによって与えられた)のログを見ると

<ion-header> 
... 
</ion-header> 
<ion-content > 
    <ion-card #ionCard> 
    <ion-card-content #ionCardContent> 
    </ion-card-content > 
    </ion-card> 
</ion-content> 

は:それらの両方がElementRef.nativeElement.clientWidthのために持って起こります同じ値。そして、それはおそらく良いですが、Chromeのinspectコンソールで(Stylesタブを介して)#IonCardContentpaddingがあることがわかります。 <ion-card-content>の実際の使用可能なスペースの幅は、(ElementRef.nativeElement.clientWidth - padding)です。そして、このパディングがElementRef.nativeElement属性にある場所が見つかりません。

ElementRef.nativeElement.styleを見ると、すべての属性に空の文字列値(0​​)があります。

私もContentChildで試してみました(これらの行は私のコードでコメントされており、対応するインポートはファイルの上にあります)。 <ion-card><ion-card-content>は標準のDOM構文ではないので、試してみる価値があると思います。しかし、その場合は、ログにundefinedが表示されます。

誰でも、Angular2のElementRef.nativeElementのパディング属性に到達する方法を知っていますか?

+0

http://stackoverflow.com/questions/5227909/get-element-padding-value-using-javascript –

+1

@GünterZöchbauerあなたは私を解決策に導きます:[ウィンドウオブジェクト] .getComputedStyle(ionCardContent.nativeElement、null ).paddingLeft);私にパディングを残す(接尾辞はpx)。あなたは答えとしてそれを書いていますか? – nyluje

答えて

3

(これはGünterZöchbauerに感謝します)。

解決策は以下のとおりです。[window object].getComputedStyle(ionCardContent.nativeElement,null).‌​paddingLeft;

それはPXサフィックスで左パディングを提供します。

getComputedStyle()の機能についての詳細はthis threadです。

関連する問題