问题
Why this example on TypeSctipt with ag-grid isn't work?
There is error in console:
EXCEPTION: No Directive annotation found on AgGridNg2
Code of cars.ts with poblem line:
import {Component, View} from 'angular2/angular2'
import {CarsService} from './cars_service'
@Component({
selector: 'cars',
bindings: [CarsService]
})
@View({
template: `
<ag-grid-ng2 id="cars" class="ag-fresh"
[column-defs]="columnDefs" [row-data]="rowData">
</ag-grid-ng2>
`,
////////////////////////////////////
// Problem at this line
////////////////////////////////////
directives: [ag.grid.AgGridNg2]
})
export class Cars {
private columnDefs: Object[];
private rowData: Object[];
constructor(service: CarsService) {
this.columnDefs = [
{ headerName: "Make", field: "make" },
{ headerName: "Model", field: "model" },
{ headerName: "Price", field: "price" }
];
service.getCars().subscribe(
(res: Response) => {
this.rowData = res.json();
//console.log(this.rowData);
// TODO: Refresh table
});
}
}
Must I import AgGridNg2 or not and how to do it? Or maybe current version of Angular2 Beta is broken?
回答1:
I had a look at your plunkr. In beta versions, angular2/angular2
is now angular2/core
.
That said, it seems that you don't correctly initialize ag-grid. You should try something like that:
ag.grid.initialiseAgGridWithAngular2({ core: core });
bootstrap(App, [
HTTP_BINDINGS,
ROUTER_BINDINGS,
bind(ROUTER_PRIMARY_COMPONENT).toValue(App),
bind(LocationStrategy).toClass(HashLocationStrategy)
]);
and remove this block from your index.html
file:
System.import("angular2/angular2").then(function(ng2) {
ag.grid.initialiseAgGridWithAngular2(ng2);
});
Here is a sample of use of ag-grid with TypeScript: https://github.com/helix46/ag-grid-angular2-beta-ts. Here is the file regarding configuration: https://github.com/helix46/ag-grid-angular2-beta-ts/blob/master/src/boot.ts.
Hope it helps you, Thierry
来源:https://stackoverflow.com/questions/34893234/exception-no-directive-annotation-found-on-aggridng2