2017-06-08 15 views
0

ない.searchは、私は、各リチウムをクリックしたときに警告を表示しようとしていますが、私はエラーキャッチされない例外TypeError:(t.data ||「」)私は、角に新しいです機能

uncaught TypeError: (t.data || "").search is not a function

を取得しています

あなたはそれを修正する方法を教えてもらえますか?

関連するコードと例:

http://plnkr.co/edit/M05wS03ynxX812u2MATc?p=preview&open=app%2Fapp.component.ts

import { Component } from '@angular/core'; 

@Component({ 
    selector: 'my-app', 

    styles: [` 
    li { 
     color: red; 
    } 
    `], 
    template: `<ul class= "master"> 
       <li *ngFor ="let student of liArraycontent" (click) = "liClicked(this)"> {{student}} </li> 
       </ul>` 
}) 
export class AppComponent { 
    name = 'Angular'; 
    liArraycontent = ["testing 1", "testing 2", "testing3"] 
    this.liClicked = function(e1) { 
    alert('a'); 
    } 


} 


/* 
Copyright 2017 Google Inc. All Rights Reserved. 
Use of this source code is governed by an MIT-style license that 
can be found in the LICENSE file at http://angular.io/license 
*/ 
+0

リンクしたplunkrはあなたの質問にエラーを表示しませんが、いくつかのタイプトスクリプトのコンパイルエラーがあります。コンパイルエラーを最初に修正し、まだ「関数ではない」エラーが表示されるかどうかを確認します。 – Duncan

答えて

0

あなたはtypescriptですクラスthis必要はありません。コンポーネント内のthisを削除するだけです。あなたはtemplateイベントでthisを使用する場合

liClicked = function(e1) { 
    alert(e1); 
} 

も、あなたはjavascriptの異なるAppComponentを取得します。現在のli要素にアクセスする場合は、代わりに$event.targetを使用してください。

Plunker demoを参考にしてください。

BTW私が提供した例のエラー(t.data || "").search is not a functionはありませんでした。多分あなたはもっと説明が必要です。

関連する問題