正确的方法,使在angularJs工厂模块 [英] Correct way to make a factory module in angularJs

查看:276
本文介绍了正确的方法,使在angularJs工厂模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的控制器功能:

I have a controller function like this:

$scope.localTimezone = function (userTimezone,datetime) {
  // ....
  return data;
}

什么是让工厂模块的正确方法是什么?我试过以下,但它给错误。

What is the correct way to make it a factory module? I tried the following but it's giving errors.

angular.module('localTimezone',  [])
   .factory('localTimezone',function(userTimezone,datetime) {
      // ...
      return data;
   });


angular.module('app', ['localTimezone'])
   .controller('tasksController',function ($scope,localTimezone) {
     // ...
   });

我错过了一些概念或logic.Can有人请点我朝着正确的方向?

I am missing out on some concept or logic.Can anyone please point me in the right direction?

推荐答案

控制器示例
不好:

CONTROLLER Example Bad:

function MainCtrl () {
  this.doSomething = function () {

  };
}
angular
  .module('app')
  .controller('MainCtrl', MainCtrl);

function MainCtrl (SomeService) {
  this.doSomething = SomeService.doSomething;
}
angular
  .module('app')
  .controller('MainCtrl', MainCtrl);

厂范例
不好:

Factory Example Bad:

function AnotherService () {

  var someValue = '';

  var someMethod = function () {

  };

  return {
    someValue: someValue,
    someMethod: someMethod
  };

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

function AnotherService () {

  var AnotherService = {};

  AnotherService.someValue = '';

  AnotherService.someMethod = function () {

  };

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

有关详细指引通过这个博客:
自以为是AngularJS风格指南为球队

For Detail Guidelines go through this blog : Opinionated AngularJS styleguide for teams

这篇关于正确的方法,使在angularJs工厂模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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