微信分享动态设置title desc取不到值的原因及解决方法

最近用angular做微信的分享,发现动态去赋值分享接口的title desc都不管用 后来百度了一下,微信分享的title会读取html里<title></title>里的内容,而我在title里写了{{title}},所以每次分享 分享的标题都是{{title}},解决方法是

//设置页面标题
document.title=$scope.title;
//增加页面元素变动,从而保证标题一定会刷新//解决问题:由于部分手机浏览器bug,导致标题更新后不会自动刷新var $body = $('body');var $iframe = $("<iframe style='display:none;' src='/favicon.ico'></iframe>");$iframe.on('load',function() { setTimeout(function() { $iframe.off('load').remove(); }, 0);}).appendTo($body);
这样做了之后还是不行,

我的代码是:

wx.ready(function () {
    wx.onMenuShareAppMessage({
        title:$scope.title,
        desc: $scope.desc,
        link:$scope.link,
        imgUrl:$scope.imgUrl,
        success: function (res) {
            alert('已分享');
            $scope.isShare=false;
            $scope.$apply()

        },
        cancel: function (res) {
            alert('已取消');
            $scope.isShare=false;
            $scope.$apply()
        },
        fail: function (res) {
            $scope.isShare=false;
            $scope.$apply()
        }
    });
})

我猜测在调用分享接口时并没有读取到$scope.title $scope.desc等数据,

后来我就把微信的配置和分享封装到一个方法中,然后把分享需要的参数作为方法的参数传入,这样在调用风险方法时就可以百分百保证数据是成功读取到的

$scope.wechat=function(title,desc,link,imgUrl){
    var ua = window.navigator.userAgent.toLowerCase();
    if(ua.match(/MicroMessenger/i) == 'micromessenger'){
        var timestamp=Math.round(new Date().getTime()/1000);
        var url=window.location.href;
        var noncestr="dianjingwang";
        var signature='',appId="";
        $.ajax({
            type: 'POST',
            url: '/wx/sign.json',
            data: {timestamp:timestamp,url:url,noncestr:noncestr},
            dataType: 'json',
            async :false,  //默认为true 异步
            success:function(data){
                if(data.msg="success"){
                    appId=data.ret;
                    signature=data.value;
                }
            }
        });
        wx.config({
            debug: false,
            appId:appId,
            timestamp:timestamp,
            nonceStr:"dianjingwang",
            signature:signature,
            jsApiList: [
                'checkJsApi',
                'onMenuShareTimeline',
                'onMenuShareAppMessage',
                'onMenuShareQQ',
                'onMenuShareWeibo',
                'onMenuShareQZone',
                'hideAllNonBaseMenuItem',
                'showAllNonBaseMenuItem',
                'closeWindow'
            ]
        });
        wx.ready(function () {
            wx.onMenuShareAppMessage({
                title:title,
                desc: desc,
                link:link,
                imgUrl:imgUrl,
                success: function (res) {
                    alert('已分享');
                    $scope.isShare=false;
                    $scope.$apply()

                },
                cancel: function (res) {
                    alert('已取消');
                    $scope.isShare=false;
                    $scope.$apply()
                },
                fail: function (res) {
                    $scope.isShare=false;
                    $scope.$apply()
                }
            });

           wx.onMenuShareTimeline({
               title:title,
               desc: desc,
               link:link,
               imgUrl:imgUrl,
               success: function (res) {
                   alert('已分享');
                   $scope.isShare=false;
                   $scope.$apply()
               },
               cancel: function (res) {
                   alert('已取消');
                   $scope.isShare=false;
                   $scope.$apply()
               },
               fail: function (res) {
                   alert(JSON.stringify(res));
                   $scope.isShare=false;
                   $scope.$apply()
               }
           });
           wx.onMenuShareQQ({
               title:title,
               desc:desc,
               link:link,
               imgUrl:imgUrl,
               complete: function (res) {
                   $scope.isShare=false;
                   $scope.$apply()
               },
               success: function (res) {
                   alert('已分享');
                   $scope.isShare=false;
                   $scope.$apply()
               },
               cancel: function (res) {
                   alert('已取消');
                   $scope.isShare=false;
                   $scope.$apply()
               },
               fail: function (res) {
                   alert(JSON.stringify(res));
                   $scope.isShare=false;
                   $scope.$apply()
               }
           });
           wx.onMenuShareWeibo({
               title:title,
               desc:desc,
               link:link,
               imgUrl:imgUrl,
               complete: function (res) {
                   alert(JSON.stringify(res));
                   $scope.isShare=false;
                   $scope.$apply()
               },
               success: function (res) {
                   alert('已分享');
                   $scope.isShare=false;
                   $scope.$apply()
               },
               cancel: function (res) {
                   alert('已取消');
                   $scope.isShare=false;
                   $scope.$apply()
               },
               fail: function (res) {
                   alert(JSON.stringify(res));
                   $scope.isShare=false;
                   $scope.$apply()
               }
           });
           wx.onMenuShareQZone({
               title:title,
               desc: desc,
               link:link,
               imgUrl:imgUrl,
               complete: function (res) {
                   $scope.isShare=false;
                   $scope.$apply()
               },
               success: function (res) {
                   alert('已分享');
                   $scope.isShare=false;
                   $scope.$apply()
               },
               cancel: function (res) {
                   alert('已取消');
                   $scope.isShare=false;
                   $scope.$apply()
               },
               fail: function (res) {
                   alert(JSON.stringify(res));
                   $scope.isShare=false;
                   $scope.$apply()
               }
           });
        });
    }

这样就完美的解决了这个问题。记录一下。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值