MOCKSTACKS
EN
Questions And Answers

More Tutorials









Angular JS with TypeScript


The way the $scope is injected in the controller's constructor functions is a way to demonstrate and use the basic option of angular dependency injection but is not production ready as it cannot be minified. Thats because the minification system changes the variable names and anguar's dependency injection uses the parameter names to know what has to be injected. So for an example the ExampleController's constructor function is minified to the following code.

function n(n){this.setUpWatches(n)
and $scope is changed to n!
to overcome this we can add an $inject array(string[]). So that angular's DI knows what to inject at what position
is the controllers constructor function.
So the above typescript changes to
module App.Controllers {
 class Address {
 line1: string;
 line2: string;
 city: string;
 state: string;
 }
 export class SampleController {
 firstName: string;
 lastName: string;
 age: number;
 address: Address;
 setUpWatches($scope: ng.IScope): void {
 $scope.$watch(() => this.firstName, (n, o) => {
 //n is string and so is o
 });
 };
 static $inject : string[] = ['$scope'];
 constructor($scope: ng.IScope) {
 this.setUpWatches($scope);
 }
 }
}


Conclusion

In this page (written and validated by ) you learned about AngularJS JS with TypeScript . What's Next? If you are interested in completing AngularJS tutorial, your next topic will be learning about: AngularJS Controllers in Typescript.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.