后台传回的json数据含有html标签,无法在wxml正确显示的

2017-02-12  本文已影响348人  jlnbda3488375

插件地址:https://github.com/kevenfeng/html-to-wxml
Solution:
1. 引用插件 htmlToWxml.js

    // xxx.js
    var R_htmlToWxml = require('../../util/htmlToWxml.js');//引入公共方法

2. 将html内容转成json数据

    // xxx.js
    onLoad:function(){
        var that = this;
        wx.request({
            url: '', 
            data: {},
            header: {'content-type': 'application/json'},
            success: function(res) {
                console.log(res.data);
                var sherry=res.data;
                that.setData({
                    silen:R_htmlToWxml.html2json(sherry.post.content)
                })
            }
        });
      }

3.页面显示

    <!-- xxx.wxml -->
    <block wx:for="{{silen}}"  wx:for-index="idy"  wx:for-item="cellData">
            <block  wx:if="{{cellData.type == 'view'}}">
                <view class="p">
                    <block  wx:for="{{cellData.child}}" wx:key="text">
                        <block  wx:if="{{item.type == 'a'}}">
                            <text class="a" data-seccode="{{item.attr['data-seccode']}}" data-secname="{{item.attr['data-secname']}}" bindtap="stockClick">{{item.text}}</text>
                        </block>
                        <block  wx:else>
                            <text>{{item.text}}</text>
                        </block>
                    </block>
                </view>
            </block>
            <block wx:if="{{cellData.type == 'img'}}">
                <image class="img" data-index="{{idy}}" style="height: {{cellData.attr.height?cellData.attr.height:0}}px"  mode="aspectFit" src="{{cellData.attr.src}}" bindload="imageLoad"></image>
            </block>
    </block>        

此方法亲测可用(pmSilen 17.02.12),再次感谢 kevenfeng

上一篇 下一篇

猜你喜欢

热点阅读