AngularJS服务不作为单身人士

时间:2016-05-24 16:38:22

标签: angularjs angularjs-service amplitude

为代码繁重的帖子道歉,但我想提供尽可能多的上下文。我在Angular.js应用程序中定义服务时遇到问题。服务应该在整个应用程序中充当单身人士(source),所以我很难得到以下行为。

在我的 app.js 文件中,我运行 AmplitudeService 服务和console.log(AmplitudeService)。这将输出一个对象,其中包含我在AmplitudeService.js文件中定义的所有方法。因此,我能够按预期正确使用服务和日志事件。

但是,当我在 header.js 中的console.log(AmplitudeService)时,它会输出我的Window对象。因此,Window不包含诸如“logEvent”,“identifyUser”等功能,因此在这种情况下 AmplitudeService 不可用。

非常感谢所有见解!

AmplitudeService.js source

注意:如果检查作者的语法,他会在服务结束时返回一个对象。在我的研究中,我已经读过在定义服务函数(source)时使用“this”关键字,并且您不需要像使用Factory那样返回对象,所以我已经更新了它相应

angular.module('AmplitudeService', [])
.service('AmplitudeService', 
['$amplitude', '$rootScope', 'amplitudeApiKey', '$location',
 function ($amplitude, $rootScope, amplitudeApiKey, $location) {

  this.init = function() {
    $amplitude.init(amplitudeApiKey, null);
    $amplitude.logEvent('LAUNCHED_SITE', {page: $location.$$path});
  }

  this.identifyUser = function(userId, userProperties) {
    $amplitude.setUserId(userId);
    $amplitude.setUserProperties(userProperties);
  }

  this.logEvent = function(eventName, params) {
    $amplitude.logEvent(eventName, params);
  }
}]);

angular-amplitude.js source

这允许在整个应用程序中访问“$ amplitude”

(function(){
var module = angular.module('angular-amplitude', ['ng']);

module.provider('$amplitude', [function $amplitudeProvider() {
    this.$get = ['$window', function($window) {
      (function(e,t){
        var r = e.amplitude || {};
        var n = t.createElement("script");
        n.type = "text/javascript";
      n.async = true;
      n.src = "https://d24n15hnbwhuhn.buttfront.net/libs/amplitude-2.2.0-min.gz.js";
      var s = t.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(n,s);
      r._q = [];

      function a(e){
        r[e] = function(){
          r._q.push([e].concat(Array.prototype.slice.call(arguments,0)));
        }
      }
      var i = ["init","logEvent","logRevenue","setUserId","setUserProperties","setOptOut","setVersionName","setDomain","setDeviceId","setGlobalUserProperties"];
      for(var o = 0; o < i.length; o++){
        a(i[o])
      }
      e.amplitude = r
    }
      )(window,document);
      return $window.amplitude;
    }];
}]);
return module;
}());

App.js

angular.module('app', [
'ngRoute',
'angular-amplitude',
'AmplitudeService',
])

 .run(['AmplitudeService', function(AmplitudeService){
 console.log(AmplitudeService); // Outputs 'Object {}'
 AmplitudeService.init();
 AmplitudeService.logEvent('LAUNCHED_SITE');
 console.log(AmplitudeService); // Outputs 'Object {}'
 }])

Header.js

angular.module('app.common.header', [])
.controller('HeaderCtrl', [ '$rootScope', '$scope', '$location', '$scope', '$route', '$window', 'AmplitudeService', function($rootScope, $scope, $location, $route, $window, AmplitudeService){

$scope.goToSearch = function(term) {
$location.path('/search/' + term);
 console.log(AmplitudeService); // Outputs 'Window {}'
};
}]);

1 个答案:

答案 0 :(得分:3)

看起来您在控制器中注入$scope,导致变量的意外映射

angular.module('app.common.header', [])
.controller('HeaderCtrl', [ '$rootScope', '$scope', '$location', **'$scope'**, '$route', '$window', 'AmplitudeService', function($rootScope, $scope, $location, $route, $window, AmplitudeService){

$scope.goToSearch = function(term) {
$location.path('/search/' + term);
 console.log(AmplitudeService); // Outputs 'Window {}'
};
}]);

如果在定义控制器时发现这种格式更容易阅读/使用

angular
  .module('app.common.header', [])
  .controller('HeaderCtrl', headerController);

headerController.$inject = [ '$rootScope', '$scope', '$location', '$route', '$window', 'AmplitudeService']

function headerController($rootScope, $scope, $location, $route, $window, AmplitudeService){
  $scope.goToSearch = function(term) {
    $location.path('/search/' + term);
    console.log(AmplitudeService); // Outputs 'Window {}'
  };
}