在 MEANJS 0.4.2 中使用 Angular 服务

Using Angular Service in MEANJS 0.4.2

我有一个可能很简单的问题。我之前在 Angular 中使用过服务,但现在 运行 使用 MEANJS Yeoman Generator 项目遇到了问题。我需要做的是在另一个模块中使用来自特定模块的数组数据,这样我就可以在另一个模型的视图中重复这个。

我到底应该在服务中的什么地方引入数组?

(function () {
  'use strict';

  angular
  .module('patients')

  .factory('PatientsService', PatientsService);

  PatientsService.$inject = ['$resource'];

  function PatientsService($resource) {
    return $resource('api/patients/:patientId', {
      patientId: '@_id'
    }, {
      update: {
        method: 'PUT'
      }
    });
  }
})();

到目前为止,我在 MEANJS 文档中没有发现任何东西,这里也没有(仅来自具有其他服务结构的旧 MEANJS 版本)。

以下是我想带入服务的内容:

  // Shows a List of useable avatars on Patient creation
    $scope.avatars = [
      { value:'1', name: 'modules/patients/client/img/avatar/avatar1.png' },
      { value:'2', name: 'modules/patients/client/img/avatar/avatar2.png' },
      { value:'3', name: 'modules/patients/client/img/avatar/avatar3.png' },
      { value:'4', name: 'modules/patients/client/img/avatar/avatar4.png' },
      { value:'5', name: 'modules/patients/client/img/avatar/avatar5.png' },
      { value:'6', name: 'modules/patients/client/img/avatar/avatar6.png' }
    ];

我想在 home.client 视图中使用头像,PatientsService 已经注入到 home.client 控制器中。

你上面的服务只是return一个$resource。相反,该服务可以 return 具有各种属性的普通旧 Javascript 对象(或 class)。其中有一个 属性 包含头像数组,另一个包含 $resource:

(function () {
  'use strict';

  angular
  .module('patients')

  .factory('PatientsService', PatientsService);

  PatientsService.$inject = ['$resource'];

  function PatientsService($resource) {
    return {
      avatars: [ {value: 0, ... } ],
      resource: $resource('api/patients/:patientId', {
          patientId: '@_id'
        }, {
          update: {
            method: 'PUT'
        }
      })
    }

  }
})();