2015-02-05 79 views
0

我得到下面的錯誤是我的AngularJs應用程序。undefined不是AngularJS中的函數錯誤

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

我不知道做一些谷歌研究之後是什麼原因。

controller.js文件

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文件:

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

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

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

    } 
} 

誤差以在controller.js文件中的下面劃線所示。

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

哪一個是在index.controller.js – 2015-02-05 17:53:09

+0

對不起@DavidGrinberg線150得到的錯誤。我現在已經更新了這個問題。顯示的錯誤在Service.share(message,subject,null,appUrl); – Purus 2015-02-05 17:54:33

回答

3

你註冊Service服務,但不能確定它的任何方法。

你也在做很多事情$rootScope,我會問你爲什麼這樣做。

不管怎麼說,你可以定義你的服務如下,你會不會在​​3210

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 
    } 
} 
+0

我的方式使用服務完成錯誤?我有其他方法在服務中,他們都工作正常。 – Purus 2015-02-05 17:57:37

+0

總之,是的,您使用該服務的方式是完全錯誤的。 '$ rootScope'應該幾乎不需要被使用 - 這就是服務的目的。 – Tom 2015-02-05 17:58:38

+0

感謝您的澄清。這是我第一個使用Angular的應用程序。所以,如果我在這個服務中有更多的功能,我應該像上面所顯示的那樣在最後完成所有的功能。是這樣嗎?你能指出我在哪裏解釋這個教程?不是官方的一位。 – Purus 2015-02-05 18:02:13

相關問題