1
서버에서 데이터를 가져와 컨트롤러에 전달하는 팩토리가 있는데 많은 컨트롤러가 팩토리에서 get 메서드를 호출하지만 팩토리를 먼저 호출하는 컨트롤러의 범위 변수는 보기 모든 변경 ... 여기 .. 여기 약속이 해결 된 후 angularjs에서 뷰가 업데이트되지 않음
(function() {
angular.module('myapp').factory('agendaFactory', ['$http', '$q', function ($http, $q) {
var user = "";
var defer = $q.defer();
var agenda = {
getAll: function (tempData, addType) {
$http.post('/api/' + addType + '/getAll', tempData).success(function (data, status) {
console.log(addType, data);
//console.log(status);
defer.resolve(data);
}).error(function (err, status) {
console.log(err, status);
defer.reject(status);
});
return defer.promise;
}
};
return agenda;
}]);
}());
그들을 호출하는 두 개의 컨트롤러입니다
changeData = function (data) {
console.log("called function in liffe");
$scope.lifetimeData = data;
}
agendaFactory.getAll(tempData, addType).then(function (data) {
console.log('lifeData', data);
changeData(data);
return;
}, function (err) {
console.log('today err', err);
})
와 두 번째 컨트롤러 내 공장 코드
changeData = function (data) {
console.log("called function in year");
$scope.yearData = data;
}
agendaFactory.getAll(tempData, addType).then(function (data) {
console.log('yeardata', data);
changeData(data);
return;
}, function (err) {
console.log('today err', err);
});
하지만 첫 번째 컨트롤러의 데이터 뷰
'$ http' 기능이 이미 약속에서 그들을 포장 할 필요는 약속을 반환하지 컨트롤러에 성공/오류 조건을 처리 할 수 있습니다. 또한'sucess'와'error'는 더 이상 사용되지 않습니다.'then'을 사용하십시오. 그리고 마지막으로, 문제를 보여주는 plunkr/fiddle을 제공 할 수 있습니까? –
@NexusDuck 이것은 큰 앱의 스 니펫입니다. 복제하는 것은 매우 어려울 것입니다. $ http 약속을 사용해야한다고 제안 했으므로 답을 쓸 수 있도록 쓰십시오. 감사합니다. –
문제를 해결하는 데 도움이됩니다. 사용하지 않는 항목을 제거하십시오. 자세한 내용은 [this] (https://docs.angularjs.org/api/ng/service/$http#deprecation-notice)를 참조하십시오. 또한, 최소한의 작업 예제 (http://stackoverflow.com/help/mcve)를 재현하여 전체 앱을 재현 할 필요가 없습니다. –