2016-06-09 99 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