Javascript 在angularjs服务中编写函数

Javascript 在angularjs服务中编写函数,javascript,angularjs,Javascript,Angularjs,我想在angularjs服务中编写一个函数,并在我所有的应用程序中重用它 控制器 var mod= angular.module('myapp', ['eventFilters', 'highlight', 'event', 'dayfilter', 'Services']). config(['$routeProvider', function($routeProvider) { $routeProvider. when('/', {templateUrl: 'template.h

我想在angularjs服务中编写一个函数,并在我所有的应用程序中重用它

控制器

var mod= angular.module('myapp', ['eventFilters', 'highlight', 'event', 'dayfilter', 'Services']).
config(['$routeProvider', function($routeProvider) {
$routeProvider.
      when('/', {templateUrl: 'template.html',   controller: Ctrl}).
      when('/temp', {templateUrl: 'temp.html',   controller: tempCtrl}).
      otherwise({redirectTo: '/invalid'});
}]);
mod.service ('sharedService', function() {
function daysInMonth(month,year) {
    return new Date(year, month+1,0).getDate();
}
});
我想在任何控制器中使用daysInMonth函数。 可能吗?如果是的话,谁能用小提琴的一些例子简要地给我解释一下


提前感谢

这里是一个基本示例,介绍如何在控制器中使用(注入)服务


希望有帮助。

将函数作为服务公开,然后让AngularJS注入器完成其余的工作。您可以轻松地将
daysInMonth
服务设置为模块中的静态值。请在下面的位置查看此操作


如果您希望一个函数可用于控制器的<强>所有< /强>,您可以考虑在$ROOTVICH上定义方法,而不是使用服务:

myApp.run(function($rootScope) {
    $rootScope.daysInMonth = function(year, month) {
        return new Date(year, month+1,0).getDate();
    }
});
然后,由于原型作用域继承,所有控制器的作用域都可以访问该方法(无需依赖项注入)。您可以在任何控制器中调用它,如下所示:

function MyCtrl($scope) {
   console.log($scope.daysInMonth(12, 2012));
}​

JavaScript将找不到$scope上定义的函数daysInMonth,因此它将在父作用域(在本例中为根作用域)中查找函数,并将找到它。

您需要公开它。将其包装在返回块中:

var mod= angular.module('myapp', ['eventFilters', 'highlight', 'event', 'dayfilter', 'Services']).
config(['$routeProvider', function($routeProvider) {
$routeProvider.
  when('/', {templateUrl: 'template.html',   controller: Ctrl}).
  when('/temp', {templateUrl: 'temp.html',   controller: tempCtrl}).
  otherwise({redirectTo: '/invalid'});
}]);
mod.service ('sharedService', function() {
   return {
      daysInMonth: function(month,year) {
         return new Date(year, month+1,0).getDate();
      }
   };
});

如果您想使用实际的角度服务(不是工厂或提供商),那么您所要做的就是按照角度的方式创建一些东西

mod.service ('sharedService', function() {
  function daysInMonth(month,year) {
    return new Date(year, month+1,0).getDate();
  }
});

// Would turn into 

mod.service("sharedService", function(){
  this.daysInMonth(month, year){
    return new Date(year, month + 1, 0).getDate();
  }
});
除非需要覆盖默认的对象,否则不应从服务返回。在这种情况下,您可以返回对象文字、函数定义、数组、大多数对象(不包括任何原语)。请确保您会问这样一个问题:“为什么我需要覆盖默认行为?”

这才是服务的关键所在。当您注入服务时Angular将新建您传递的功能,从而按说明将其返回给您。就你而言,这项服务正是你所需要的。Angular将只构建一次服务,并且新的sharedService功能结果将在整个应用程序中可用,但仅当该服务在应用程序中的某个位置至少部署一次时。如果您不DI它,那么服务将永远不会被创建,因为它是惰性加载的(工厂和服务)

如果你发现你真的想退货(尽管你可以在服务中退货),合适的地方应该是在一家有角度的工厂。事实上,如果你不从工厂回来,你会得到一个很好的通知

错误:提供程序“sharedService”必须从$get factory方法返回一个值

注意:其中$get函数是服务函数定义的第二个参数

mod.factory("sharedService", function(){
  function daysInMonth(month, year){
    return new Date(year, month + 1, 0).getDate();
  }

  return {
    daysInMonth: daysInMonth
  }

  // Or you could do this...
  return daysInMonth;
});

当使用AngularFactory时,您将不再获得NEW'ED功能,这就是必须创建退货的原因。

我认为OP意味着服务是一种功能,例如$timeout功能。由于默认服务工厂为您返回的任何内容提供“服务”,因此您可以执行以下操作:

angular.module('myApp.myModule',[])
.factory('add',['$injectables',函数($injectables){
返回函数(arg1、arg2)
{
//这是使用此服务时将调用的函数
返回arg1+arg2;
}

}]);非常感谢。实际上wat是根作用域和作用域的区别?只有一个根作用域。还有许多其他范围。ng controller、ng repeat、ng include和一些指令创建新的作用域。阅读更多内容:,根据Angular.js约定,rootScope不应用于此类内容。@dman,我大体上同意这一原则,但如果所有控制器都需要它,我可能会将其放入$rootScope(带注释)中,而不是到处注入。@Mark Rajcok,但是,如果碰巧您有一些链接到隔离作用域的控制器,那么您必须在所有这些控制器中注入$rootScope,并且最终也会注入很多,但这是一个非常通用的东西。还是我错了?value()的用法很好——我还没有见过太多这样的例子。
mod.service ('sharedService', function() {
  function daysInMonth(month,year) {
    return new Date(year, month+1,0).getDate();
  }
});

// Would turn into 

mod.service("sharedService", function(){
  this.daysInMonth(month, year){
    return new Date(year, month + 1, 0).getDate();
  }
});
mod.factory("sharedService", function(){
  function daysInMonth(month, year){
    return new Date(year, month + 1, 0).getDate();
  }

  return {
    daysInMonth: daysInMonth
  }

  // Or you could do this...
  return daysInMonth;
});