Issue
I would like to have names of parameters of a specific back end route available in my template when working with forms.
My idea is that by passing name of a route, e.g. GetReservations
, to a custom directive, I would have access to parameter names of that route, e.g. date: string
and time: string
.
<form [backend-route]="'GetReservations'" #params>
<label>Date</label>
<input [name]="params.date" type="text"/>
<label>Time</label>
<input [name]="params.time" type="text"/>
</form>
Is it possible, using this or some other way, to have a template variable params
with some desired type?
I haven't had much experience with Angular's template syntax, so I am not sure what is possible.
Solution
Answering my own question.
Instead of using an attribute directive, using a structural directive let's you declare template variables, and by using ngTemplateContextGuard
, you can set their type.
<form *backendRoute="'GetReservations'; let params;">
@Directive({selector: '[backendRoute]'})
export class RouteDirective<TRouteName extends keyof Actions & keyof ActionResults> {
@Input('backendRoute') typeToken: TRouteName | [TRouteName, (result: ActionResults[TRouteName]) => void];
constructor(private tpl: TemplateRef<Context<Actions[TRouteName]>>,
private vcr: ViewContainerRef) {
}
static ngTemplateContextGuard<TRouteName extends keyof Actions & keyof ActionResults>(dir: RouteDirective<TRouteName>,
ctx: unknown): ctx is Context<Actions[TRouteName]> {
return true
};
}
// Not yet sure how this works
class Context<T> {
$implicit!: T;
subscribe!: T;
constructor(value: T) {
this.$implicit = value;
this.subscribe = value;
}
}
Answered By - Desperado
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.