0

I am trying to use TypeScript in AngularJS development. I am new and just trying to get some basic building blocks. I started with this tutorial: http://angularfirst.com/your-first-angular-project-in-visual-studio/

After getting it to work I tried to convert it to TypeScript and this is where I have not been able to make the connection.

What I am trying to do is to create a TypeScript class that can be used as a controller, currently I am just using a simple property bag. Once I understand how to get the class to be read then I will expand my controller.

My class looks like this:

class main {
    "use strict";
    public food: string;
    constructor() {
        var vm = this;
        vm.food = "pizza";
    }
}

My controller call looks like this:

(function() {

    angular
        .module("app")
        .controller("Main",
        ["$scope", main]);
});

and my HTML:

<!DOCTYPE html>
<html ng-app="app">
<head>
    <title>Index</title>
    <meta charset="utf-8"/>

</head>
<body ng-controller="Main as vm">

    <input type="text" ng-model="vm.food" placeholder="Enter food" />

    <p ng-show="food">Sriracha sauce is great with {{vm.food}}!</p>

    <script src="Scripts/angular.min.js"></script>
    <script src="app/app.module.js"></script>
    <script src="app/main.js"></script>

</body>
</html>

My result is:

Sriracha sauce is great with {{vm.food}}!

What I expect is:

Sriracha sauce is great with pizza!

I have tried passing $scope into the constructor and have tried to understand what the $inject does but have not gotten this to work.

Any assistance would be appreciated. I am stuck and have not been able to locate an answer or example in a couple nights of search and play.

Thanks

EDIT: I have tried the comments from Kim and basarat singularly and together and neither solution changed the results.

I tried debugging in Chrome and it is not hitting the constructor.

After working through the examples and trying a few of my own thoughts and changes here is what I have.

class main {
"use strict";
public food: string;
constructor() {
    this.food = "pizza";
    //$scope.vm = this;

}
}


(function() {

angular
    .module("app",[])
    .controller("Main", function ($scope) {
        $scope.vm = new main();
    });
});
1
  • Did you try: angular.module("app",[])? Commented Sep 10, 2015 at 3:24

2 Answers 2

0

I think you did not properly construct your module. It should have [] such that:

angular.module("app", []).controller("Main", function ($scope) {
     $scope.vm = new main();
});

Also, you need to pass a function in the controller instead of your class. Inside your scope, you instantiate your class.

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

1 Comment

He also doesn't actually call that function.
0

Put the current instance on the $scope :

class main {
    "use strict";
    public food: string;
    constructor($scope) {
        $scope.vm = this;
        this.food = "pizza";
    }
}

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.