我對angularjs有點新鮮。我有一個可以正常工作的服務。但是我想將模型(患者模型)從服務中移出並放入一個separtate JavaScript文件中。不太確定如何做到這一點。我猜我需要某種類型的工廠或服務?將模型移出服務
這是我patient.service.js
(function() {
'use strict';
angular
.module('beincharge.patient')
.factory('patientDataService', patientDataService);
patientDataService.$inject = ['$http'];
function patientDataService($http) {
var service = {
getData: getData
};
return service;
//////////
function getData() {
// I would like to move this model into a separate js file
function patient(d) {
this.Status = d.Status;
this.ImageUrl = d.ImageUrl;
this.PersonId = d.PersonId;
this.LastName = d.LastName;
this.MiddleName = d.MiddleName;
this.FirstName = d.FirstName;
}
// end I would like to move this model into a separate js file
var data = [
{
"Status": "Active",
"ImageUrl": "http://lorempixel.com/100/100/people/9/",
"PersonId": 1,
"LastName": "Pratt",
"MiddleName": "B",
"FirstName": "Allie"
},
{
"Status": 'Active',
"ImageUrl": "http://lorempixel.com/100/100/people/3/",
"PersonId": 1,
"LastName": "Pratt",
"MiddleName": "B",
"FirstName": "Allie"
}];
return getPatientList(data);
function getPatientList(data) {
var a = [];
angular.forEach(data, function (d, i) {
a.push(new patient(d));
})
return a;
}
}
}
所以我想給模型進入一個名爲patient.model.js
(function() {
function patient(d) {
this.Status = d.Status;
this.ImageUrl = d.ImageUrl;
this.PersonId = d.PersonId;
this.LastName = d.LastName;
this.MiddleName = d.MiddleName;
this.FirstName = d.FirstNa
}
return patient;
}());
不知道你想做什麼。大多數情況下,服務*都是這種情況下的模型。你已經有了一個處理數據幷包含業務邏輯的單元。 – estus