我可以在我的AngularJS厂多种功能? [英] Can I have multiple functions in my AngularJS Factory?

查看:176
本文介绍了我可以在我的AngularJS厂多种功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我继教程从官方AngularJS文档,我想知道我是否可以添加其他功能电话工厂,这样我可以组织code更好。他们宣布了查询功能,但如果我想添加一个功能QUERY2引用不同的URL什么......说phones2 /?phoneName.json例如:

厂声明:

  VAR phonecatServices = angular.module('phonecatServices',['ngResource']);phonecatServices.factory('电话',['$资源',
  功能($资源){
    返回$资源('手机/:phoneId.json',{},{
      查询:{方法:GET,则params:{phoneId:'手机'},IsArray的:真正}
    });
  }]);

我已经尝试了一些东西,其中非似乎工作:■

这<一个href=\"http://stackoverflow.com/questions/17233481/angularjs-creating-multiple-factories-for-every-endpoint\">answer似乎是在正确的轨道上,但对于每个工厂函数的语法并不完全与上述工厂相匹配。

线沿线的东西:

  phonecatServices.factory('电话',['$资源',
      功能($资源){
       返回{
        查询:...
        QUERY2:...
       }
      }]);


解决方案

这方面的一个例子是:
链接演示

  angular.module('服务',[])。工厂('factoryName',[$过滤器
  功能($过滤器){
    VAR method1Logic =功能(参数){
      // code
    };
    VAR method2Logic =功能(参数){
     // code
    };
    返回{
      方法1:method1Logic,
      方法2:method1Logic
    };
  }
])。控制器('MainController',[$范围,$ rootScope,$过滤器,factoryName功能($范围,$ rootScope,$过滤器,factoryName){
     $ scope.testMethod1 =功能(ARG){
       $ scope.val1 = factoryName.method1(ARG);
     };     $ scope.testMethod2 =功能(ARG){
       $ scope.val2 = factoryName.method2(ARG);
     };
}]);

有就是这甚至一个更好的版本自以为是的版本:参考

 函数AnotherService(){  变种AnotherService = {};  AnotherService.someValue ='';  AnotherService.someMethod =功能(){  };  返回AnotherService;
}

  .module(应用)
  .factory('AnotherService',AnotherService);

I'm following the Tutorial from the official AngularJS docs and I want to know if I can add another function to the Phone factory so that I can organize code better. They have declared a "query" function, but what if I wanted to add a query2 function that references a different url...say phones2/:phoneName.json for example?

Factory declaration:

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

phonecatServices.factory('Phone', ['$resource',
  function($resource){
    return $resource('phones/:phoneId.json', {}, {
      query: {method:'GET', params:{phoneId:'phones'}, isArray:true}
    });
  }]);

I have tried a number of things and non of them seem to be working :s

This answer seems to be on the right track, but the syntax for each factory function doesn't quite match up with the above factory.

Something along the lines of:

phonecatServices.factory('Phone', ['$resource',
      function($resource){
       return {
        query: ...
        query2: ...
       }
      }]);

解决方案

One such example of this is: Link for Demo

angular.module('services', []).factory('factoryName', ["$filter",
  function($filter) {
    var method1Logic = function(args) {
      //code
    };
    var method2Logic = function(args) {
     //code
    };
    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);
     };
}]);

There is even a better version Opinionated version of this: References

function AnotherService () {

  var AnotherService = {};

  AnotherService.someValue = '';

  AnotherService.someMethod = function () {

  };

  return AnotherService;
}
angular
  .module('app')
  .factory('AnotherService', AnotherService);

这篇关于我可以在我的AngularJS厂多种功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆