2016-06-09 100 views
0

我有一个或许很简单的问题。我之前在Angular中使用过服务,但现在使用MEANJS Yeoman Generator项目遇到了问题。我需要的是从另一个模块的特定模块中使用数组的数据,以便我可以在其他模型的视图内重复这一点。在MEANJS中使用角度服务0.4.2

我究竟在服务内部引入数组?

(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控制器内部已经注入。

回答

0

上述服务仅返回$resource。相反,服务可以返回一个普通的旧的Javascript对象(或一个类),它具有各种属性。其中将包含化身的数组的属性,另一个包含$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' 
     } 
     }) 
    } 

    } 
})(); 
+0

哦,我明白了,这确实是容易的,但我想它的完满成功走错了路!非常感谢 – d8ta

+0

您是否知道我是否需要更改其他任何内容才能在整个应用程序中使用$资源。它似乎不能再使用?! – d8ta

+0

git it myself。我认为你可以同时使用它。阿凡达作为对象和资源,就像它们在对象之后的地方一样。 – d8ta