Mic*_*ael 1 javascript angularjs q
看看这个简单的例子:
function colorPromise() {
return $q.when({data:['blue', 'green']})
}
function getColors() {
return colorPromise().then(function(res) {
console.log('getColors', res)
// do something with res
};
}
function testGetColors() {
getColors().then(function(res) {
if (angular.equals(res, {data:['blue', 'green']})) {
console.log('passes')
}
});
}
Run Code Online (Sandbox Code Playgroud)
Plunker:http://plnkr.co/edit/LHgTeL9sDs7jyoS7MJTq?p = preview
在这个例子res中的testGetColors功能是undefined.
你怎么能传递res到承诺的第二个then功能$q?
你需要回来 res
function getColors() {
return colorPromise().then(function(res) {
console.log('getColors', res)
return res; // here
};
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
76 次 |
| 最近记录: |