4

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:

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

4
  • If it's relevant, Webpack (2) is being used to manage bundling. Commented Jan 31, 2018 at 11:20
  • 1
    angular 2 has something like this when compiling ahead of time. for angularjs though the templates are not being compiled at buildtime so they never get to pass trough such a check. Commented Jan 31, 2018 at 11:56
  • @toskv - I'm asking whether there is a way to compile/check AngularJS templates at build time. Commented Jan 31, 2018 at 11:58
  • I got that. And the answer is no because angularjs templates are not compiled. they only get interpreted at runtime. Commented Jan 31, 2018 at 11:59

2 Answers 2

1

No.

AngularJs templates are interpreted at runtime, there is no compilation step for them where this check could happen.

Sign up to request clarification or add additional context in comments.

1 Comment

Wondering if there might be external tooling or a compiler extension point (e.g. VS Code plugin) that parses & validates the templates (I think it's clear that this functionality isn't built into the TS compiler - it doesn't belong there). Will leave the question open for a bit longer and if no further answers will accept yours :)
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.