angular.module('services', []).factory('factoryName', ["$filter", function($filter) { var method1Logic = function(args) { //pre }; var method2Logic = function(args) { //pre }; return { method1: method1Logic, method2: method1Logic }; }]).controller('MainController', ["$scope", "$rootScope", "$filter", "factoryName", function ($scope, $rootScope, $filter,factoryName) { $scope.testMethod1 = function(arg){ $scope.val1 = factoryName.method1(arg); }; $scope.testMethod2 = function(arg){ $scope.val2 = factoryName.method2(arg); };}]);
甚至有这样一个更好的版本固执己见版
function AnotherService () { var AnotherService = {}; AnotherService.somevalue = ''; AnotherService.someMethod = function () { }; return AnotherService;}angular .module('app') .factory('AnotherService', AnotherService);
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)