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

时间:2017-03-04 09:23:44

标签: angularjs ionic-framework cordova-plugins

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

  

TypeError:无法读取属性' 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 :(得分: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;
    }
};

注意:这只适用于移动设备,而不适用于桌面浏览器。