最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Text binding between input and span in Angular - Stack Overflow

programmeradmin4浏览0评论

How can I bind input texts to span innerHTML in Angular6?

ts file

...
finance_fullname: string;
...

template file

<input type="text" id="finance_fullname" [(ngModel)]="finance_fullname">
<span class="fullname" ngBind="finance_fullname"></span>

How can I bind input texts to span innerHTML in Angular6?

ts file

...
finance_fullname: string;
...

template file

<input type="text" id="finance_fullname" [(ngModel)]="finance_fullname">
<span class="fullname" ngBind="finance_fullname"></span>
Share Improve this question edited Mar 8, 2020 at 19:09 brucelin asked Sep 30, 2018 at 16:04 brucelinbrucelin 1,0212 gold badges12 silver badges25 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 12

I can say most secure way would be innerText or textContent.

<span class="fullname" [textContent]="finance_fullname"></span>
<span class="fullname" [innerText]="finance_fullname"></span>

Even AngularJS were using textContent for one way binding. It only extract the model value and dump directly inside the specified html node. Even though if you pass html it will add that html as a text(decoded html) on the page.

Demo

innerHTML would also work for you, but it could be dangerous, as it will give a chance to inject malicious content on the page in form of html.

I don't know why you are reading from angularjs since you are working with angular 6. If you want double binding just do it like this.

<input type="text" id="finance_fullname" [(ngModel)]="finance_fullname">
<span class="fullname">{{finance_fullname}}</span>

You can do it in two ways

(i) You can use [innerHTML]

<input type="text" id="finance_fullname" [(ngModel)]="finance_fullname">
<span class="fullname" [innerHTML]="finance_fullname"></span>

STACKBLITZ DEMO

(ii) Just bind using the two way data binding

STACKBLITZ DEMO

<input type="text" id="finance_fullname" [(ngModel)]="finance_fullname">
<span class="fullname">{{finance_fullname}}</span>
发布评论

评论列表(0)

  1. 暂无评论