目前我有以下服务在我的角度应用中的其他位置填充表。
更新时间:
'use strict';
angular.module('projyApp')
.service('data', function data() {
// AngularJS will instantiate a singleton by calling "new" on this function
var getAllPeeps = function() {
return peeps;
}
var insertPeep = function(peep) {
peeps.push(peep);
}
var getPeep = function(peep) {
var foundPeep;
if (peep.firstname) {
peeps.forEach(function (pps){
if (pps.firstname == peep.firstname) {
foundPeep = pps;
}
});
} else if (peep.lastname) {
peeps.forEach(function (pps){
if (pps.lastname == peep.lastname) {
foundPeep = pps;
}
});
} else if (peep.inumber) {
peeps.forEach(function (pps) {
if (pps.inumber == peep.tagid) {
foundPeep = pps;
}
});
}
if (foundPeep) {
return foundPeep;
} else {
return "Error";
}
}
var getDataFromServer = function() {
//Fake it.
var peeps = [
{firstname:'Buster', lastname:'Bluth', tagid:'01'},
{firstname:'John', lastname:'McClane', tagid:'02'},
{firstname:'Mister', lastname:'Spock', tagid:'03'}
];
return peeps;
}
var peeps = getDataFromServer();
return {
getAllPeeps : getAllPeeps,
insertPeep : insertPeep,
getPeep : getPeep
}
});
显然,这工作,但我想要的“窥视”阵列从外部JSON文件保存的对象。
我如何保持功能的同时,从外部JSON文件加载“窥视”,像这样:
$http.get("../../TestData/peeps.json").success(function(data) {
});
控制台给我“Error:Unknown provider:$ scopeProvider < - $ scope < - data”? – captainrad
您必须向服务中注入$ scope。 –
我已经做了更新以包含更多我的代码。 – captainrad