Whisher
Whisher

Reputation: 32806

Typescript + angularjs custome filter Unknown provider:

I want to create a custom filter in angularjs using typescript my code

//Reverse.ts
class Reverse{
    static filter(msg:string) {
        return (msg) => {
            return msg.split("").reverse().join("");
        }
    }
}


//app.ts
/// <reference path="../lib/angular.d.ts"/>
/// <reference path="./Reverse.ts"/>
class MyApp{
    public app:AngularModule;
    constructor(){
          this.app = angular.module('myApp', []);
          this.app.filter ('reverse',(msg) => { return Reverse.filter(msg) });
    }
}
class AlbumController {
    constructor (private $scope) {
        this.$scope.test = '20 whatsomething';
     }
}


new MyApp();

//index.html
<!DOCTYPE html>
<html>
    <head>
        <title>My test TS</title>
    </head>
    <body>
    <div data-ng-app="myApp">
        <div data-ng-controller="AlbumController">  
            {{test | reverse}}
        </div>
    </div>
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script>
    <script src="./Reverse.js"></script>
    <script src="./app.js"></script>
    </body>
</html>

I get Error: Unknown provider: msgProvider <- msg <- reverseFilter so can you help me, please ?

UPDATE

I worked it out

class MyApp{
    public app:AngularModule;
    constructor(){
          this.app = angular.module('myApp', []);
          this.app.filter ('reverse',() => { 
            return (msg) => {
                return Reverse.filter(msg);
            } 
          });
    }
}
class AlbumController {
    constructor (private $scope) {
        this.$scope.test = '20 whatsomething';
     }
}


new MyApp();


class Reverse{
    static filter(msg:string) {
        return msg.split("").reverse().join("");
    }
}

thanks the same

Upvotes: 0

Views: 2328

Answers (1)

basarat
basarat

Reputation: 276383

Your original code would have worked if you would have compiled with the --out compiler flag :

tsc --out app.js app.ts

PS: I did a video on typescript + angularjs workflow : http://youtube.com/watch?v=0-6vT7xgE4Y&hd=1

Upvotes: 1

Related Questions