1. 程式人生 > >Vue.js -- 雙向過濾器去除html標簽

Vue.js -- 雙向過濾器去除html標簽

body htm 去除 del div box ron -m col

<div id="box">
    <input type="text" v-model="msg | filterHtml">
    <br>
    <label v-html="msg"></label>
</div>

js:

new Vue({
    el:‘#box‘,
    data:{
        msg:‘<strong>這是段文字!</strong>‘//定義一段文字
   }
});

Vue.filter(‘filterHtml‘,{
    read:function
(val){//val就是以獲取的msg的值 return val.replace(/<[^>]*>/g);//去除文字的<...></...>標簽 }, write:function(){ return val; } });

Vue.js -- 雙向過濾器去除html標簽