Я пытаюсь создать директиву с контроллером с использованием Angular 1.5 и TypeScript 1.8, но это не сработает для меня. Я посмотрел на кучу примеров, но я следую этим примерам в тройник, и это все равно не сработает для меня. Я проклят?Угловая директива 1.5 + TypeScript с контроллером
Вот код:
export class FooController implements angular.IController {
static $inject = ['myService', '$state'];
constructor(private myService: Services.MyService, private $state: angular.ui.IStateService) {
}
public foo: Services.Dtos.Foo;
public bar;
$onInit() {
if (!this.foo) return;
this.bar = this.$state.href('app.bar', {id: this.foo.id});
}
}
export class FooDirective implements angular.IDirective {
public restrict = 'E';
public replace = true;
public scope = {};
public templateUrl = 'content/templates/foo.template.html';
public controller = FooController;
public controllerAs = 'ctrl';
public bindToController = {
foo: '<',
};
}
Она ошибки на FooDirective
говорят следующее:
- Класс 'FooDirective' неправильно 'реализует интерфейс IDirective.
- Типы собственности «bindToController» несовместимы.
- Тип '{foo: string; } 'не присваивается type' boolean | {[boundProperty: string]: string; }».
- Тип '{foo: string; } 'не присваивается типу' {[boundProperty: string]: string; }».
- Недопустимая подпись подписи '{foo: string; }».
Что я делаю неправильно?
UPDATE 2017/02/15 IDirective выглядит следующим образом (из angular.d.ts файла):
interface IDirective {
compile?: IDirectiveCompileFn;
controller?: string | Injectable<IControllerConstructor>;
controllerAs?: string;
/**
* @deprecated
* Deprecation warning: although bindings for non-ES6 class controllers are currently bound to this before
* the controller constructor is called, this use is now deprecated. Please place initialization code that
* relies upon bindings inside a $onInit method on the controller, instead.
*/
bindToController?: boolean | {[boundProperty: string]: string};
link?: IDirectiveLinkFn | IDirectivePrePost;
multiElement?: boolean;
priority?: number;
/**
* @deprecated
*/
replace?: boolean;
require?: string | string[] | {[controller: string]: string};
restrict?: string;
scope?: boolean | {[boundProperty: string]: string};
template?: string | ((tElement: JQuery, tAttrs: IAttributes) => string);
templateNamespace?: string;
templateUrl?: string | ((tElement: JQuery, tAttrs: IAttributes) => string);
terminal?: boolean;
transclude?: boolean | 'element' | {[slot: string]: string};
}
Я скопировал ваш код в проект Angular и не получил ошибку. Какие типизации для 'IDirective' вы используете? Пожалуйста, добавьте их на свой вопрос. –
@ Сабастин, я добавил тимпинг. Это очень озадачивает меня. Я не уверен, почему это не работает. –
В соответствии с этим: http://stackoverflow.com/questions/22077023/why-cant-i-indirectly-return-an-object-literal-to-satisfy-an-index-signature- это решение явно cast scope и bindToController к '{[boundProperty: string]: string}', и ошибка исчезнет. –