Q 구현 $ q에 대한 AngularJS의 설명서에 대한 도움말을 따르고 있습니다. 나는 나의 이해는 scope.apply 여기에 $ 콜백에게 각도의 컨텍스트를주고 $ 범위에서 확인 변수를하게 접근 할 수 있다는 것이다 https://docs.angularjs.org/api/ng/service/$q
// for the purpose of this example let's assume that variables `$q`, `scope` and `okToGreet`
// are available in the current lexical scope (they could have been injected or passed in).
function asyncGreet(name) {
var deferred = $q.defer();
setTimeout(function() {
// since this fn executes async in a future turn of the event loop, we need to wrap
// our code into an $apply call so that the model changes are properly observed.
scope.$apply(function() {
deferred.notify('About to greet ' + name + '.');
if (okToGreet(name)) {
deferred.resolve('Hello, ' + name + '!');
} else {
deferred.reject('Greeting ' + name + ' is not allowed.');
}
});
}, 1000);
return deferred.promise;
}
var promise = asyncGreet('Robin Hood');
promise.then(function(greeting) {
alert('Success: ' + greeting);
}, function(reason) {
alert('Failed: ' + reason);
}, function(update) {
alert('Got notification: ' + update);
});
에서 다음 코드를 시도했다.
그러나 아래에 $의 Q와 크리스 Kowal의의 Q 비교에
는, 테스트 코드가 간다 :it('should simulate promise', inject(function($q, $rootScope) {
var deferred = $q.defer();
var promise = deferred.promise;
var resolvedValue;
promise.then(function(value) { resolvedValue = value; });
expect(resolvedValue).toBeUndefined();
// Simulate resolving of promise
deferred.resolve(123);
// Note that the 'then' function does not get called synchronously.
// This is because we want the promise API to always be async, whether or not
// it got called synchronously or asynchronously.
expect(resolvedValue).toBeUndefined(); // <= so the deferred is not resolved without the 'apply'?
// Propagate promise resolution to 'then' functions using $apply().
$rootScope.$apply();
expect(resolvedValue).toEqual(123);
}));
은 $ rootScope 말한다 $ 적용() 약속 해상도 '다음'을 전파하는 것입니다.. 나는 혼란 스럽다 ... 적용을 사용하지 않고, 연기 된 해석은 실제로 해결되지 않을 것인가?
감사합니다. 귀하의 설명에 많은 도움이됩니다. :) – lkahtz