현재 내 각진 앱의 다른 곳에 테이블을 채우는 다음 서비스가 있습니다.
업데이트 :
'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) {
});
콘솔에 "오류 : 알 수없는 공급자 : $ scopeProvider <- $ scope <- 데이터"가 표시됩니까? – captainrad
$ scope를 서비스에 삽입해야합니다. –
내 코드를 더 많이 포함하도록 업데이트했습니다. – captainrad