2017-12-20 6 views
0

私はこのビデオhereを検証してAngular 2でフォームを作成していましたが、この人がどのように入力フィールドにURLの先頭を含めていたか本当に知りたいです。私は彼の同じコードに従っており、デフォルトで "http://"セクションを表示することはできません。角度2のフォームにプリセットテキストを追加するにはどうすればよいですか?

これは私がcompany.company_url値は、コンポーネントの最初のレンダリングの前に初期化されていたよう

<label>Company URL <span class="required">*</span></label> 
<input class="form-control" [(ngModel)]="company.company_url" name="url" #url="ngModel" type="url" pattern="https?://.+" id="url" required maxlength="255"> 
<div *ngIf="companyErrors?.url" class="alert alert-field alert-danger"> 
    {{companyErrors.url}} 
</div> 

答えて

0

が見える持っているものです。あなたは、コンポーネントのコンストラクタに割り当てをしようとする場合があります。

constructor(/*...*/) { 
    // ... 
    company.company_url = "http://"; 
} 

または多分より良いNgInit上:

ngOnInit() { 
    // ... 
    company.company_url = "http://"; 
} 
0

別のアプローチは、あなたのinputタグにplaceholder属性を利用されるだろう。この

<input class="form-control" [(ngModel)]="company.company_url" name="url" #url="ngModel" type="url" pattern="https?://.+" id="url" required maxlength="255" placeholder="http://"> 

よう

何かが同じ効果をレンダリングします。

関連する問題