在angularjs服务中编写函数

JSA*_*ict 13 angularjs

我想在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();
}
});
Run Code Online (Sandbox Code Playgroud)

我想在我的任何控制器中使用daysInMonth函数.可能吗?如果有的话,任何人都可以用小提琴中的一些例子简要地解释我

提前致谢

Ber*_*and 26

这里提供了一个如何在控制器中使用(注入)服务的基本示例.

http://jsfiddle.net/uhmNR/1/

var myApp = angular.module('myApp',[]);


//Here is the service Users with its functions and attributes
//You can inject it in any controller, service is a singleton and its data persist between controllers
myApp.factory('Users', function () {

    var userName = "John Doe";

    return {
        getUserName: function () {
             return userName;                   
        },
        setUserName: function (newName) {
            userName = newName;
        }
    }
});

//An Util service with DaysInMonth method   
myApp.factory('Util', function () {

    return {
        daysInMonth: function (month,year) {

            return new Date(year, month+1,0).getDate();
        }
    };

});   

//Here I am injecting the User service andusing its methods   
myApp.controller('MyCtrl', ['$scope', 'Users', 'Util', function ($scope, Users, Util) {

    Users.setUserName('Robin Hood');

    $scope.name = Users.getUserName();

    //Using Util.daysInMonth()
    $scope.date = Util.daysInMonth(12,2012);
}]);
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你.


Hum*_*rto 6

将该功能作为服务公开,然后让AngularJS注入器完成其余工作.您可以轻松地将daysInMonth服务设置为模块中的静态值.在http://jsfiddle.net/hbulhoes/zdtnw/上查看此操作

var mod = angular.module('myapp', []);

// This is the declaration of the daysInMonth service. It's set as
// a value in the module, with the value being the very function
// you want to share among other services and controllers:
mod.value('daysInMonth', function(month, year) {
    return new Date(year, month+1,0).getDate();
});

// And this is an example controller that depends on the daysInMonth function.
function MyController($scope, daysInMonth){
    $scope.DaysInCurrentMonth = daysInMonth(12, 2012);
}
Run Code Online (Sandbox Code Playgroud)


Mar*_*cok 1

如果您希望某个函数可供所有控制器使用,您可以考虑在 $rootScope 上定义该方法,而不是使用服务:

\n\n
myApp.run(function($rootScope) {\n    $rootScope.daysInMonth = function(year, month) {\n        return new Date(year, month+1,0).getDate();\n    }\n});\n
Run Code Online (Sandbox Code Playgroud)\n\n

然后,由于原型作用域继承,所有控制器的作用域都可以访问该方法(无需依赖注入)。您可以在任何控制器中调用它,如下所示:

\n\n
function MyCtrl($scope) {\n   console.log($scope.daysInMonth(12, 2012));\n}\xe2\x80\x8b\n
Run Code Online (Sandbox Code Playgroud)\n\n

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

\n

  • 根据 Angular.js 约定,rootScope 不应该用于此类内容。 (10认同)