我想创建一个自定义过滤器 在angularjs中使用typescript 我的代码
//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>
我收到错误:未知提供者:msgProvider&lt; - msg&lt; - reverseFilter 所以,你能帮助我吗?
更新
我解决了
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("");
}
}
感谢同样的
答案 0 :(得分:1)
如果您使用--out编译器标志编译,原始代码将起作用:
tsc --out app.js app.ts
PS:我做了关于打字稿+ angularjs工作流程的视频:http://youtube.com/watch?v=0-6vT7xgE4Y&hd=1