我试图把我的第一个角度组件与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',因为它是异步的。看到这个笨蛋。 –