Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I am Playing around with Type Script.I have convert my angular js controller to Type Script But i m facing problem in ng-repeater. (I have attached my controller code below:-

class CustomCtrl{
    public customer;
    public ticket;
    public services;
    public cust_File;
    public ticket_file;
    public service_file;

    static $inject = ['$scope', '$http', '$templateCache'];
    constructor (
            private $http,
            private $templateCache
    ){}
share|improve this question
1  
So, I checked/fixed plunker, plnkr.co/edit/3XORgParE2v9d0OVg515?p=preview and it should work and show that all in action.. hope it helps – Radim Köhler May 28 '15 at 12:53
    
Great Help!!!! Thanksssss @RadimKöhler – Shian JA May 28 '15 at 12:57
up vote 2 down vote accepted

I decided to add another answer describing more details how to create and use controller in TypeScript and inject it into angularJS.

This is extension of this Answer

How can I define my controller using TypeScript? Where we also have a working plunker

So having this directive:

export class CustomerSearchDirective implements ng.IDirective
{
    public restrict: string = "E";
    public replace: boolean = true;
    public template: string = "<div>" +
        "<input ng-model=\"SearchedValue\" />" +
        "<button ng-click=\"Ctrl.Search()\" >Search</button>" +
        "<p> for searched value <b>{{SearchedValue}}</b> " +
        " we found: <i>{{FoundResult}}</i></p>" +
        "</div>";
    public controller: string = 'CustomerSearchCtrl';
    public controllerAs: string = 'Ctrl';
    public scope = {};
}

We can see, that we declared this directive to be available as Element. We also in-lined a template. This template is ready to bind SearchedValue and call Action on our controller Ctrl.Search(). We are saying what is the name of controller: 'CustomerSearchCtrl' and asking runtime to make it available as 'Ctrl' (conrollerAs:)

Finally we inject that object into angular module:

app.directive("customerSearch", [() => new CustomerSearch.CustomerSearchDirective()]);

We can use $scope as ng.IScope, but to have more typed access to it, we can create our own interface:

export interface ICustomerSearchScope  extends ng.IScope
{
    SearchedValue: string;
    FoundResult: string;
    Ctrl: CustomerSearchCtrl;
}

This way, we know, that we have string SearchedValue and also other string FoundResult. We also informed the application that Ctrl will be injected into that scope, and will be of type CustomerSearchCtrl. And here comes that controller:

export class CustomerSearchCtrl
{
    static $inject = ["$scope", "$http"];
    constructor(protected $scope: CustomerSearch.ICustomerSearchScope,
        protected $http: ng.IHttpService)
    {
        // todo
    }
    public Search(): void
    {
        this.$http
            .get("data.json")
            .then((response: ng.IHttpPromiseCallbackArg<any>) =>
            {
                var data = response.data;
                this.$scope.FoundResult = data[this.$scope.SearchedValue]
                    || data["Default"];
            });
    }
}

plus its registration into module

app.controller('CustomerSearchCtrl',  CustomerSearch.CustomerSearchCtrl);

What is interesting on this controller? it has one public acton Search, which has access to all its membes via this., e.g. this.$http. Because we instructed intellisense in VS that angular.d.ts type/interface

protected $http: ng.IHttpService

will be used, we can later easily access its methods. Similar is the type of returned value in .then()

.then((response: ng.IHttpPromiseCallbackArg<any>) => {...

which does contain data: {} of any type...

Hope it helps a bit, observe that all in action here

share|improve this answer
    
If that helped... great. Enjoy amazing combination of TypeScript and AngularJS (and version 2.0 will be even better;) – Radim Köhler May 28 '15 at 12:59
    
Kohler getting status as 400 – Shian JA Jun 2 '15 at 13:11
    
Rise new question, would be my suggestion. It does not make sense to extend these, already solved. You will get larger audience... and higher probability to get answer.. – Radim Köhler Jun 2 '15 at 13:13
    
Please Review Question Getting Error in Angular js + Type Script in http put status 400 – Shian JA Jun 2 '15 at 13:14

There is one issue with your constructor and $inject - these must fit together

// wrong
static $inject = ['$scope', '$http', '$templateCache'];
constructor (
        private $http,
        private $templateCache
){}

// should be
static $inject = ['$scope', '$http', '$templateCache'];
constructor (
        private $scope,
        private $http,
        private $templateCache
){}

What happened in fact - all params were moved in the meaning, that $http was $scope in fact, etc...

Simply, $inject array MUST fit to constructor parameter list

BTW, that's why I had previously here: http://stackoverflow.com/a/30482388/1679310 suggested to use types in the declaration:

   constructor(protected $scope: ICustomerScope,
        protected $http: ng.IHttpService,
        protected $templateCache: ng.ITemplateCacheService)
    { ... }
share|improve this answer
    
Thanks for good suggestion but Done with that too still same problem. – Shian JA May 28 '15 at 9:32
    
What is the error you are provided with? could you place it in the question? – Radim Köhler May 28 '15 at 9:32
    
Kohler I have created two sepearte file one as my controller and second the module code provided by you but it's giving me error of ng is not found i have attached a jpeg with my question – Shian JA May 28 '15 at 10:01
1  
You'd need d.ts file with angular stuff. The latest could be found here github.com/borisyankov/DefinitelyTyped/blob/master/angularjs/…, Also read this to get more details how these files do work definitelytyped.org – Radim Köhler May 28 '15 at 10:03
    
Kohelr where i have to add this below code:- var app = angular.module("MyControllerModule"); app.controller("CustomerCtrl", MyModule.CustomerCtrl); } – Shian JA May 28 '15 at 10:28

From quick review of your code I have found that search method of controller is private. May be changing it to public will solve problem.

share|improve this answer
    
Nups Problem Remains Same – Shian JA May 28 '15 at 9:19

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.