我正在尝试将我的第一个角度组件与 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 仅包含带有一些随机文本的 ap 元素。
我可以在调试时看到我正在检索数据,但我无法在组件控制器上访问它......
编辑:感谢下面接受的答案,我已经解决了我的问题。它与异步调用的问题无关,而是与我定义路由和组件的方式有关。请参阅下面的代码进行修复。再次感谢。