Description
Problem
Assume the following code example:
@Component({
template: `
<ul>
<li *ngFor="let user of users; let i = index">{{i + 0}}: {{user.last}}, {{user.first}}<li>
<ul>
`
})
class MyExample {
users: List<User> = [];
}
class User {
first: string;
last: string;
}
Here the IDE has type information for users, but it does not have type information for user and i. For this reason it can not verify:
- that
i+0is valid because it does not know thatiis a number. - that
user.lastanduser.firstare valid because it does not know what is the type foruser
Solution
Parameterize TemplateRef<T>.
class Row {
$implicit: any;
index: number;
even: boolean;
odd: boolean;
}
@Directive({
selector: '[ng-for][ng-for-of]',
})
class NgFor {
constructor(templateRef: TemplateRef<Row>, viewContainer:ViewContainer) {
}
}
Changing the View API
Currently setting the local variables is done as:
var view: ViewRef;
view.setLocal('$implicit', item)
A better way to do this, which is more consistent with the above proposal is to inject a locals object during view Creation.
var local: Row = new Row(item, index);
var view: ViewRef<Row> = this._viewContainer.createEmbeddedView(templateRef, index, local);
The advantage is that currently ngFor has to compute odd, even regardless of wether it is needed in the template. By injecting a local's object the odd field can be computed.
class Row implements Locals {
constructor(public $implicit: T, index: number) {}
get even() => this.index % 2 == 0;
get odd() => this.index % 2 == 1;
}
This means that the Row object can have a rich API, but unless a field is actually referenced from the template it has no cost of computing these fields. Furthermore by using a typed object we can get better performance on locals then when using hashes.
Add Type to the templates
Requires: read generics from DI dependencies (constructor arguments).
import {User} from '...';
@Component({
types: {User}
template: `
<ul>
<li *ngFor="let user: User of users; let i = index">{{i + 0}}: {{user.last}}, {{user.first}}<li>
<ul>
`
})
class MyExample {
users: List<User> = [];
}Notice the types in the @Component which sets up a map of template types to real types. This will allow the compiler to generate code such as this.
Generated code:
// Deduce generic Row from NgFor constructor asking for a TemplateRef<Row>
class MyExample_View1<Row> extends AppView<Row> {
...
changeDetection() {
var user: User /* retrieved from the Template */ = context.$implicit;
var i = context.index; // Here i is number because context.index is number.
}
}
class AppView<T> {
context: T
}
class TemplateRef<T> {
appView: AppView<T>;
get context(): T {
return this.appView.context;
}
}
Activity
scheglov commentedon Oct 21, 2015
There are couple of things I don't understand about the proposed solution.
LocalesFactorywith a type argument, likeRowin case ofng-for?@Input() in: Array<T>;? CurrentlyNgFordeclares theng-for-ofproperty. I expected to see it instead ofin.Localssubclass fields?mhevery commentedon Oct 21, 2015
LocalesFactoryto give IDE hints.of.scheglov commentedon Oct 21, 2015
No, it's not a problem.
I just want to make sure that I correctly understand the solution and how you see it to be used.
Thank you.
I think this will give the Angular2 analysis plugin enough type information.
tbosch commentedon Oct 23, 2015
@scheglov Could you have another look? We just simplified the proposal to not rely on a new interface
LocalsFactorybut rather to makeViewContainerbe parameterized on the locals type. Would this also work?scheglov commentedon Oct 23, 2015
Yes, this works too.
19 remaining items
tbosch commentedon Jun 23, 2016
The missing part can be done post final as it is not a breaking change.
mhevery commentedon Aug 4, 2016
I think #10510 is a better solution to this problem.
chuckjaz commentedon Oct 19, 2016
Closed in favor of #12398
angular-automatic-lock-bot commentedon Sep 10, 2019
This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.