AngularJS模板可以使用TypeScript进行类型安全吗?(Can AngularJS templates use TypeScript for type

编程入门 行业动态 更新时间:2024-10-26 20:28:10
AngularJS模板可以使用TypeScript进行类型安全吗?(Can AngularJS templates use TypeScript for type-safety?)

假设Angular 1.6应用程序使用最佳实践(组件/绑定到控制器尽可能),我们是否可以在模板中利用TypeScript的强大功能?

例如,如果我们有一些模板代码

<div>{{$ctrl.children[0].name}}</div>

我们知道控制器的类型($ ctrl) - 我们可以让TypeScript给我们一个错误

$ ctrl上不存在'children' '孩子'不是一个阵列 '孩子'中的物品没有物业'名称'(等)?

VS Code中的代码完成也很不错。

我见过以下问题:

https://github.com/Microsoft/TypeScript/issues/6508 https://github.com/Microsoft/TypeScript/issues/5151

然而,很难从事情所在的地方得出结论(特别是在AngularJS 1.x世界中)。

Assuming an Angular 1.6 app using best practices (components/bind-to-controller where possible), can we harness the power of TypeScript in templates?

For example if we have some template code

<div>{{$ctrl.children[0].name}}</div>

& we know the type of the controller ($ctrl) - can we have TypeScript give us an error if

'children' does not exist on $ctrl 'children' is not an array the items in 'children' do not have a property 'name' (etc)?

Code completion in VS Code would be nice too.

I've seen the issues below:

https://github.com/Microsoft/TypeScript/issues/6508 https://github.com/Microsoft/TypeScript/issues/5151

However it's difficult to conclude from that where things are at (especially in the AngularJS 1.x world).

最满意答案

没有。

AngularJs模板在运行时被解释,没有编译步骤可以进行此检查。

As an alternative you could start writing Angular 2 components and then downgrade them to AngularJS using @angular/upgrade/static::downgradeComponent downgradeComponent

更多推荐

TypeScript,$ctrl,'children',issues,Code,电脑培训,计算机培训,IT培训"/>

本文发布于:2023-07-16 12:34:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1128529.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:可以使用   模板   类型   AngularJS   type

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!