2017-09-20 4 views
0

は私の問題の短い説明:私は私のHTMLにコード行を持っていた今までAngular4は非同期値にカスタムパイプを追加

は:

<pre class="preFormattedText" [innerHtml]="(project | async)?.Remarks"></pre> 

は、私は便利なプラグイン Linkifyとでこの記事を見つけました。だから私は、新しいpipeComponentを追加しました:

import { Pipe, PipeTransform } from '@angular/core'; 

@Pipe({ name: 'linkify' }) 
export class LinkifyPipe implements PipeTransform { 
    transform(link: string): string { 
     return this.linkify(link); 
    } 
private linkify(plainText): string { 
    let replacedText; 
    let replacePattern1; 
    let replacePattern2; 
    let replacePattern3; 

    //URLs starting with http://, https://, or ftp:// 
    replacePattern1 = /(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gim; 
    replacedText = plainText.replace(replacePattern1, '<a href="$1" target="_blank">$1</a>'); 

    //URLs starting with "www." (without // before it, or it'd re-link the ones done above). 
    replacePattern2 = /(^|[^\/])(www\.[\S]+(\b|$))/gim; 
    replacedText = replacedText.replace(replacePattern2, '$1<a href="http://$2" target="_blank">$2</a>'); 

    //Change email addresses to mailto:: links. 
    replacePattern3 = /(([a-zA-Z0-9\-\_\.])[email protected][a-zA-Z\_]+?(\.[a-zA-Z]{2,6})+)/gim; 
    replacedText = replacedText.replace(replacePattern3, '<a href="mailto:$1">$1</a>'); 

    return replacedText; 
    } 
} 

だから私は

<pre class="preFormattedText" [innerHtml]="(project | async)?.Remarks | linkify"></pre> 

にコードの私の元の行を更新しました。しかし、今、私はこのエラーを取得:

ERROR TypeError: Cannot read property 'replace' of null

と私のコードのこの部分ハイライト表示されます

<h3 class="subtitleH3">{{ this.localizationService.localized['globalComment'] }}</h3> 
<div> 
    <pre class="preFormattedText" [innerHtml]="(project | async)?.Remarks | linkify"></pre> 
</div> 
+0

最初の置換または2番目の置換はnullを返します。あなたはどれを見つけることができますか? –

+0

あなたの非同期データを変換したいのであれば、あなたのパイプがplainTextの値を得ていないように見える理由は何ですか(プロジェクト| async | linkify) – JayDeeEss

+0

@RobinDijkhofパイプ内にブレークポイントを設定すると、指定されたobjはnullで、かつてはそのコンテンツをリンクしたいと思っています...私はなぜコンテンツを非同期にしているのでしょうか? –

答えて

0

見つけました。単純なヌルチェックを実装する必要がありました。

関連する問題