私はいくつかのデータを解決する必要があるui.router状態を持っていますが、コントローラをロードしてからテンプレートをロードする前に解決するようです。コンポーネントの角度分解
状態の定義は次のようになります。
$stateProvider
.state('route', {
url: '/update/:id',
template: '<update order="$resolve.order"></update>',
resolve: {
order: function(OrdersService, $stateParams) {
return OrdersService.get($stateParams.id).$promise
}
}
})
コンポーネント:
const Update = {
bindings: {
order: "<"
},
controller,
template
};
angular.module('app')
.component('update', Update);
コントローラー:
export default class UpdateCtrl {
constructor(){
...
console.log(this.order);
...
}
}
OrdersService:
class OrdersService {
constructor($resource, api_name) {
this.$resource = $resource(api_name + '/orders/:id', { id: '@id' }, {
get: { method: 'GET' }
});
}
get(orderId) {
return this.$resource.get({ orderId });
}
}
angular.module('app')
.factory('OrdersService', [
'$resource',
'api_name',
($resource, api_name) => new OrdersService($resource, api_name)
]);
すべての輸入は大丈夫です、と私はまだ、コントローラから、次の取得、およびテンプレート
console.log(this.order) // undefined
P.S.で完全なデータコントローラのコンストラクタでconsole.log(this.order,this)
を呼び出すと、私はいくつかの奇妙な動作を見つけました。
それは
undefined OrderUpdateCtrl
$stateParams:Object
OrdersService:OrdersService
order:Resource // full resolved object
__proto__:Object
解決されたときに待機する$ onChangesフックを使用する必要がありますか? – Andrea