2017-03-22 16 views
0

私はウェブプロジェクトを使用し、角度2を使用していますクリックイベントを使用した別のページは問題ありませんが、新しいページが作成された後、イベントをクリックすると新しいページでエラーが発生します。角2のクリックイベントが機能しない

このエラー

TypeError: self.parentView.parentView.context.changedItem is not a function at View_SiteConfig3.handleEvent_0 (/AppModule/SiteConfig/component.ngfactory.js:137) at View_SiteConfig3.eval (core.umd.js:12399) at HTMLTextAreaElement.eval (platform-browser.umd.js:3223) at ZoneDelegate.invokeTask (zone.js:265) at Object.onInvokeTask (core.umd.js:3971) at ZoneDelegate.invokeTask (zone.js:264) at Zone.runTask (zone.js:154) at HTMLTextAreaElement.ZoneTask.invoke (zone.js:335)

サイトconfig.html

<div *ngFor="let item of items1; let i = index;" class="col-sm-4"> 
    <button (click)="changedItem(item)">Test</button> 
</div> 

サイトconfig.ts

import { Component, OnInit } from '@angular/core' 
import { Http } from '@angular/http' 

@Component({ 
    selector: 'site-config', 
    moduleId: module.id, 
    templateUrl: '/site-config.html' 
}) 
export class SiteConfig implements OnInit { 

    items1: number[] = [1,2,3] 

    ngOnInit(): void { 
    } 

    public changedItem(item) { 
     //My Codes This Here 
    } 

    constructor(private http: Http) { 
    } 
} 

EDIT:この問題を解決しました。

My project worked. Problem is ts file not compiled then give a "is not a function" error

+0

changedItemメソッドを公開できますか? –

+0

changedItemメソッドのみが動作せず、app.tsファイルなしで上記のすべてのコード –

答えて

2

は多分それが作品からのエラーですあなたが提供しないコードの

私はあなたのコードを再現:

ビュー:

<pre *ngIf="current">{{current}}</pre> 
<div *ngFor="let item of items1; let i = index;" class="col-sm-4"> 
    <button (click)="changedItem(item)">Test</button> 
</div> 

クラス内容:

items1: number[] = [1,2,3] 
    current: number; 
    ngOnInit() { 
    } 

    public changedItem(item) { 
     this.current = item; 
    } 

    constructor() { 
    } 

はここで働いてPlunkrです:https://plnkr.co/edit/ZGI1FNB8RWN9BnnPnKgJ?p=preview

あなたはより多くのコードを提供することができますか?

+0

私のプロジェクトが機能しました。問題はコンパイルされていないので、 "関数ではありません"というエラーです。助けてくれてありがとう –

+1

問題はありません!トピックを閉じるには、この回答に「受け入れ済み」とマークしてください。ハッピーコーディング! –

0

これを試してみてください:

サイトconfig.html

<div *ngFor="let item of items1; let i = index;" class="col-sm-4"> 
    <button type="button" (click)="changedItem($event, item)">Test</button> <!-- add type="button" --> 
</div> 

サイトconfig.ts

import { Component, OnInit } from '@angular/core' 
import { Http } from '@angular/http' 

@Component({ 
    selector: 'site-config', 
    moduleId: module.id, 
    templateUrl: '/site-config.html' 
}) 
export class SiteConfig implements OnInit { 

    items1: number[] = [1,2,3] 

    ngOnInit(): void { 
    } 

    public changedItem(event: any, item: number) { 
     event.preventDefault(); 
     //Your code... 
    } 

    constructor(private http: Http) { 
    } 
} 
関連する問題