我試圖把我的第一個角度組件與ngRoute放在一起,到目前爲止我無法獲取數據來解決。 配置:角度組件綁定未定義
.when('/myfirstcomponent', {
template: '<myfirstcomponent claimKeys="$resolve.claimKeys"></myfirstcomponent>',
resolve: {
claimKeys: ['$http', function($http) {
$http.get('server/claimkeys.json').then((response) => {
var claimKeys = response.data.DATASET.TABLE;
return claimKeys;
});
}]
}
})
組件:
.component('myfirstcomponent', {
bindings: {
'claimKeys': '@'
},
templateUrl: 'components/component.html',
controller: [function() {
this.$onInit = function() {
var vm = this;
console.log(vm.claimKeys);
};
}]
該組件的HTML只是有這一切一些隨機文本p元素。
我可以看到在調試時,我檢索數據,但我不能訪問它的組件控制器上......
編輯:感謝接受的答案下面我有固定我的問題。它與異步調用的問題沒有任何關係,但是與我如何定義我的路由和組件有關。請參閱下面的代碼修復。再次感謝。
那麼我的第一個問題是在我的模板'claimKeys'應該是索賠密鑰。然而,這只是解決了一個字符串'$ resolve.claimKeys'...一點點進展,但沒有得到任何進一步的。 – Mickers
可能的重複[如何返回來自異步調用的響應?](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) –
'return claimKeys'不會將任何內容返回給'$ resolve.claimKeys',因爲它是異步的。看到這個笨蛋。 –