Javascript 将模型移出服务
我对angularjs有点陌生。我的服务很好。但是,我想将模型和患者模型移出服务,并将其放入一个单独的javascript文件中。不太清楚怎么做。我想我需要一个工厂或某种类型的服务 这是我的patient.service.jsJavascript 将模型移出服务,javascript,angularjs,viewmodel,angularjs-service,angularjs-factory,Javascript,Angularjs,Viewmodel,Angularjs Service,Angularjs Factory,我对angularjs有点陌生。我的服务很好。但是,我想将模型和患者模型移出服务,并将其放入一个单独的javascript文件中。不太清楚怎么做。我想我需要一个工厂或某种类型的服务 这是我的patient.service.js (function () { 'use strict'; angular .module('beincharge.patient') .factory('patientDataService', patientDataService); patient
(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;
}());
如果您只需要一个可以实例化和重用的模型,那么在patientModel.js文件中创建一个简单的JavaScript对象,如下所示:
function PatientModel() {
this.Status = "";
this.ImageUrl = "";
this.PersonId = "";
this.LastName = "";
this.MiddleName = "";
this.FirstName = "";
}
确保在要使用该文件的控制器或服务之前加载该文件
然后,您可以像这样实例化它:
var patient = new PatientModel();
angular
.module('beincharge.patient')
.factory('Patient', function() {
return function(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
}
});
angular
.module('beincharge.patient')
.factory('patientDataService', patientDataService);
patientDataService.$inject = ['$http', 'Patient'];
function patientDataService($http, Patient){
console.log(new Patient({ Status: 'active' }));
}
当然,如果您需要的话,您可以将构造函数更改为具有一些参数,这样您就可以传递d参数中的任何参数
只要您只需要一些简单的和可重用的模型,这将对您有效。优点是,您可以在测试中继续使用它们,而不是依赖硬编码的json对象。您正在尝试用java风格编写javascript,我认为没有必要在这里创建患者模型,您案例中的响应/数据与患者对象完全相同。一个更简单的实现是
(function () {
'use strict';
angular
.module('beincharge.patient')
.factory('patientDataService', patientDataService);
patientDataService.$inject = ['$http'];
function patientDataService($http) {
var service = {
getData: getData
};
return service;
this.getData = function () {
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 data;
};
}
在这里你可以移动app.service。。进入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;
}());
var app=角度。模块“beincharge_patient”,[];
应用程序服务'patientData',功能{
var getP=functionad{
此状态=d状态;
this.ImageUrl=d.ImageUrl;
this.PersonId=d.PersonId;
this.LastName=d.LastName;
this.MiddleName=d.MiddleName;
this.FirstName=d.FirstName;
}
var服务={
getP:getP
}
回程服务;
};
app.factory'patientDataService',['http','patientData',函数$http,patientData{
var getData=函数{
风险值数据=[{
状态:活动,
图像URL:http://lorempixel.com/100/100/people/9/,
人名:1,,
姓:普拉特,
姓名:B,
名字:艾莉
},
{
状态:“活动”,
图像URL:http://lorempixel.com/100/100/people/3/,
人名:1,,
姓:普拉特,
姓名:B,
名字:艾莉
}
];
var getPatientList=函数数据{
var a=[];
角度数据,函数d,i{
a、 pushnew patientData.getPd;
}
返回a;
}
返回getPatientListdata;
}
var服务={
getData:getData
};
回程服务;
}];
app.controller'beincharge_patient_controller',['$scope','patientDataService',function$scope,patientDataService{
$scope.temp=patientDataService.getData;
}];
{{temp}}
您必须创建工厂提供程序,并应如下所示:
var patient = new PatientModel();
angular
.module('beincharge.patient')
.factory('Patient', function() {
return function(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
}
});
angular
.module('beincharge.patient')
.factory('patientDataService', patientDataService);
patientDataService.$inject = ['$http', 'Patient'];
function patientDataService($http, Patient){
console.log(new Patient({ Status: 'active' }));
}
然后在服务中,您可以这样使用:
var patient = new PatientModel();
angular
.module('beincharge.patient')
.factory('Patient', function() {
return function(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
}
});
angular
.module('beincharge.patient')
.factory('patientDataService', patientDataService);
patientDataService.$inject = ['$http', 'Patient'];
function patientDataService($http, Patient){
console.log(new Patient({ Status: 'active' }));
}
您可以看到下面的完整示例:
不知道你想做什么。在这种情况下,大多数情况下服务都是一个模型。您已经有了一个处理数据和包含业务逻辑的单元。谢谢,这看起来很像我想要的。今晚我会试一试,如果一切顺利的话,我会赏金的。