AngularJS - 在 angular 中声明服务的不同方式的主要区别是什么? [英] AngularJS - what are the major differences in the different ways to declare a service in angular?

查看:24
本文介绍了AngularJS - 在 angular 中声明服务的不同方式的主要区别是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个 angularJS 应用程序,我正在努力坚持使用 AngularJs 中最有效和最广为接受的开发风格.目前,我正在使用这种方式来声明我的服务:

I am working on an angularJS app and I am trying to stick with the most efficient and widely accepted styles of development in AngularJs. Currently, I am using this way of declaring my services like so:

app.factory('MyService', function() {
  /* ... */
  function doSomething(){
     console.log('I just did something');

  }

  function iAmNotVisible(){
      console.log('I am not accessible from the outside');
  }
  /* ... */

  return{
     doSomething: doSomething
  };
});

然而,有很多例子,我不太确定要遵循哪种设计风格.对服务有广泛了解的人能否解释为什么某种风格比另一种风格更相关?

However, there are numerous examples out there and I am not quite sure which design style to follow. Can someone with extensive knowledge about services explain the reason why a certain style is more relevant than another?

除了限制对我服务中某些功能的访问之外,我所做的是否有任何用处?

Is what I am doing useful in any way other than restricting the access to certain functions in my service?

推荐答案

我建议您像在 angular-seed 应用,除了该应用烦人地只在 服务中使用 value.js 样板.但是,您可以调整它们用于控制器、指令和过滤器的布局.

I would suggest you layout your factory or service as they do in the angular-seed app, except that app annoyingly only uses value in the services.js boilerplate. However you can adapt the layout they use for controllers, directives and filters.

'use strict';

/* Filters */

angular.module('myApp.filters', []).
  filter('interpolate', ['version', function(version) {
    return function(text) {
      return String(text).replace(/\%VERSION\%/mg, version);
    }
  }]);

这意味着您将要做的服务:

Which means for a service you would do:

'use strict';

/* Services */

angular.module('myApp.filters', []).
  service('myservice', ['provider1', 'provider2', function(provider1, provider2) {
      this.foo = function() { 
          return 'foo'; 
      };
  }]).
  factory('myfactoryprovider', ['myservice', function(myservice) {
       return "whatever";
  }]);

这比您绝对需要的样板文件多,但它是压缩安全的,并保持您的命名空间干净.

This has more boilerplate than you absolutely need, but it is minification safe and keeps your namespaces clean.

你所要做的就是决定constvaluefactoryservice中的哪一个最适合合适的.如果您想创建单个对象,请使用 service:它作为构造函数被调用,因此您只需将任何方法分配给 this,所有内容都将共享相同的对象.如果你想完全控制一个对象是否被创建,尽管它仍然只被调用一次,请使用 factory.使用 value 返回一个简单的值,使用 const 作为您可以在 config 中使用的值.

Than all you have to do is decide which of const, value, factory or service is most appropriate. Use service if you want to create a single object: it gets called as a constructor so you just assign any methods to this and everything will share the same object. Use factory if you want full control over whether or not an object is created though it is still only called once. Use value to return a simple value, use const for values you can use within config.

这篇关于AngularJS - 在 angular 中声明服务的不同方式的主要区别是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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