首页 文章

TypeError:无法读取未定义的属性'socialsharing'

提问于
浏览
2

在离子上制作app并获得错误:

TypeError:无法读取undefined的属性'socialsharing'

请检查我的代码我做了什么错误 . 制作一个简单的图像共享应用程序,我在社交共享插件上获取错误,请检查下面的代码,并建议我应该怎么做才能纠正这个问题 .

.controller('imageCtrl',function($scope,$cordovaSocialSharing,$http,$window) {
    $scope.myImages = [
        {id :'1',image:'1.jpg'},
        {id :'2',image:'2.jpg'},

    ];

    $scope.getImagePath = function(imageName) {
        return "img/" + imageName.image;
    }

    $scope.doit= function(index){
        console.log(index);

    }
    $scope.share = function(t, msg, img, link){
        if(t == 'w')
            window.plugins.socialsharing
                .shareViaWhatsApp(msg, img, link);
        else if(t == 'f')
            window.plugins.socialsharing
                .shareViaFacebook(msg, img, link);
        else if(t == 't')
            window.plugins.socialsharing
                .shareViaTwitter(msg, img, link);
        else if(t == 'sms')
            window.plugins.socialsharing
                .shareViaSMS(msg+' '+img+' '+link);
        else
        {
            var sub = 'Beautiful images inside ..';
            window.plugins.socialsharing
                .shareViaEmail(msg, sub, '');
        }
    }
});

App.js

angular.module('starter', ['ionic','app.controllers','ngCordova'])

.run(function($ionicPlatform) {
  $ionicPlatform.ready(function() {
    if(window.cordova && window.cordova.plugins.Keyboard) {
      // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
      // for form inputs)
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);

      // Don't remove this line unless you know what you are doing. It stops the viewport
      // from snapping when text inputs are focused. Ionic handles this internally for
      // a much nicer keyboard experience.
      cordova.plugins.Keyboard.disableScroll(true);
    }
    if(window.StatusBar) {
      StatusBar.styleDefault();
    }
  });
})

HTML部分

<ion-content ng-controller="imageCtrl">
    <div ng-repeat="myImage in myImages" ng-click="doit($index)">
        {{myImage.id}}
        <img class ="col  item item-image thumbnail" ng-src="{{getImagePath(myImage)}}"/>
        <div class="card gallary item item-divider">
            <div class="item item-text-wrap row">
                <button ng-click="share('w', '', myImage.image, '');"
                        class="button button-light col col-20">
                    <i class="icon ion-social-whatsapp"></i>
                </button>
                <button ng-click="share('f', 'myMessage', myImage.image, '');"
                        class="button button-light col col-20">
                    <i class="icon ion-social-facebook"></i>
                </button>
                <button ng-click="share('t', 'myMessage', myImage.image, '');"
                        class="button button-light col col-20">
                    <i class="icon ion-social-twitter"></i>
                </button>
            </div>
        </div>
    </div>
</ion-content>

检查图片
enter image description here

1 回答

  • 0

    通过使用 $cordovaSocialSharing 服务尝试就像它的documented . 请注意 shareViaEmail() 应为 canShareViaEmail() . 您可能会使用 switch 而不是此 if - if else - if else - if else - else 模式:

    $scope.share = function(t, msg, img, link){
        switch (t) {
            case "w":
                $cordovaSocialSharing.shareViaWhatsApp(msg, img, link);
                break;
            case "f":
                $cordovaSocialSharing.shareViaFacebook(msg, img, link);
                break;
            case "t":
                $cordovaSocialSharing.shareViaTwitter(msg, img, link);
                break;
            case "sms":
                $cordovaSocialSharing.shareViaSMS(msg + ' ' + img + ' ' + link);
                break;
    
            default:
                $cordovaSocialSharing.canShareViaEmail(msg, 'Beautiful images inside ..', '');
                break;
        }
    };
    

    Note :这只适用于移动设备,而不适用于桌面浏览器 .

相关问题