0

I am getting the below error is my AngularJs app.

 TypeError: undefined is not a function
    at Scope.$rootScope.shareImageNow (index.controller.js:150)

I am not sure what's the reason after doing some Google research.

controller.js File

app.controller('ViewCtrl', function($rootScope, $state, $scope, $ionicPlatform, $ionicPopup, $ionicLoading, $ionicSlideBoxDelegate, $ionicScrollDelegate, $timeout, Service) {

    var appUrl = "https://play.google.com";

    $rootScope.shareNow = function() {

        var message = "xx";
        var subject = "yy";

        Service.share(message, subject, null, appUrl);

    }

    $rootScope.shareImageNow = function() {

        var imageIndex = 0;

        var imgUrl = $rootScope.itemData[imageIndex]['url'];

        var message = "xx";
        var subject = "yy";

        Service.share(message, subject, imgUrl, appUrl);

    }
});

service.js File:

module.service('Service', function($rootScope, $ionicPlatform, $cordovaSocialSharing) {

    $rootScope.share = function(message,subject,file,link) {

        $ionicPlatform.ready(function() {
            $cordovaSocialSharing
                .share(message, subject,file,link);
        }, false);

    }
}

The error is shown at the below line in controller.js file.

Service.share(message, subject, null, appUrl);
2
  • Which one is line 150 in the index.controller.js Commented Feb 5, 2015 at 17:53
  • Sorry @DavidGrinberg. I have updated the question now. The error shown is at Service.share(message, subject, null, appUrl); Commented Feb 5, 2015 at 17:54

1 Answer 1

3

You're registering the Service service, but not defining any methods on it.

You're also doing a lot of things with $rootScope, which I'd question why you're doing that.

Anyways, you can define your service as follows, and you will not get the error at Service.share()

module.service('Service', function($ionicPlatform, $cordovaSocialSharing) {

  var share = function(message,subject,file,link) {
      $ionicPlatform.ready(function() {
          $cordovaSocialSharing
              .share(message, subject,file,link);
      }, false);

  };

  return {
      share: share
  }
}
Sign up to request clarification or add additional context in comments.

5 Comments

Is my way of using a service complety wrong? I have other methods in the service and they all work fine.
In short, yes, the way you're using the service is completely wrong. $rootScope should almost never need to be used -- that's what services are for.
Thanks for clarification. This is my first app using Angular. So, if I have more functions in the service, I should be returning all of them at the end as shown. is that so? Can you point me to a tutorial where this is explained? not the official one plz.
See this jsBin. I tried to demonstrate a little. Notice in service, there are two functions, but I only return one. I'm able to call what I've returned, share(), but unable to call privateFunction, as it is not exposed publicly. www.egghead.io is a good place to start for free AngularJS tutorials
Thanks @tom. I will check on this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.