2017-12-11 4 views
0

@HostBinding、@HostListener、および@Inputの組み込みに問題があります。画像エリアの上にマウスを置いてホバリングすると、画像間を交換することになっています。それはうまくいくはずだが、そうではないようだ。どんな助けもありがとうございます。Angularアプリで@HostBinding、@HostListener、および@Inputが機能しないのはなぜですか?

これは、無料で高い評価を受けている本の「Angular 4:From Theory to Practice」から抜粋したものです。 「カスタムディレクティブ」の章の「章の終わり」のアクティビティです。

は、ここで私が問題の際、現在のよどこのための私のplunkerリンクです:http://plnkr.co/edit/MO2m8F4A3PLIIzMIbMD5?p=preview

、ここでは、コードはここにもある:

import {platformBrowserDynamic} from '@angular/platform-browser-dynamic'; 
import { 
    Component, 
    Directive, 
    Renderer, 
    HostListener, 
    HostBinding, 
    ElementRef, 
    NgModule, 
    Input, 
    Output, 
    EventEmitter 
} from '@angular/core'; 
import {BrowserModule} from '@angular/platform-browser'; 


@Directive({ 
    selector: "[ccRollover]" 
}) 
class RolloverImageDirective { 
    constructor(private el: ElementRef, private renderer: Renderer){ 

    } 
    @HostBinding('src') private image: string; 

    @Input('ccRollover') images: Object = { 
    initial: 'https://unsplash.it/200/300?image=201', 
    over: 'https://unsplash.it/200/300?image=202' 
    }; 
    @HostListener('mouseover') onMouseOver() { 
    this.image = this.images.over; 
    } 
    @HostListener('mouseout') onMouseOut(){ 
    this.image = this.images.initial; 
    } 
    //TODO: Flesh out this directive 
    //TODO: HINT - Use ngOnChanges() 
} 

@Component({ 
    selector: 'app', 
    template: ` 
<img ccRollover[images]="{ 
    initial:'https://unsplash.it/200/300?image=201', 
    over:'https://unsplash.it/200/300?image=202' 
}"/> 
` 
}) 
class AppComponent { 
} 

@NgModule({ 
    imports: [BrowserModule], 
    declarations: [ 
    AppComponent, 
    RolloverImageDirective 
    ], 
    bootstrap: [AppComponent] 
}) 
export class AppModule { 
} 

platformBrowserDynamic().bootstrapModule(AppModule); 
+2

http:// pl nkr.co/edit/pWrPm6ErxU4S57dGrn8l?p=preview – yurzui

+0

あなたは何をしようとしていますか? – Aravind

+0

@yurzuiプランナーリンクを使用していただきありがとうございます。私はHostBindingを組み込んだ方法を除いて、変更したものすべてを理解することができます。私が試したように、文字列変数に直接バインドするのではなく、並べ替えの関数を実行するのはなぜですか?あなたのソリューションの_image変数からバインドする方法を理解できません。ありがとう。 –

答えて

0

@Input('images') images: Object = {..} //<--images, not ccRollover 

です

<img ccRollover [images]="{..} //<--see the "space" between ccRollover and [images] 
+0

私は両方の変更を取り入れましたが、それでも動作しません。 リンク先の「プランナー」ページに移動して、自分の考えを送信する前に、ソリューションを試すことができます。 –

+0

申し訳ありません@ルーン、私はやったことがあります。私はもう一度やりました。とにかく、yurzuiのplunkerを確認してください – Eliseo

関連する問題