Skip to content

Commit 4561544

Browse files
author
500Tech
committed
added Injectable decorator
1 parent b361c63 commit 4561544

File tree

4 files changed

+27
-13
lines changed

4 files changed

+27
-13
lines changed

src/components/comment-list/comments.component.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* Created by voland on 4/2/16.
33
*/
4-
import {Component} from '../../decorators';
4+
import {Component, Injectable} from '../../decorators';
55
import './comment-list.scss';
66
import {IComment} from "../../interfaces";
77
import {CommentComponent} from "../comment/comment.component";
@@ -24,17 +24,18 @@ import {FilterByTagsPipe} from "../../pipes/filterByTags";
2424
</div>
2525
</div>`
2626
})
27+
28+
@Injectable()
2729
export class CommentsComponent {
2830
comments: IComment[];
2931
emptyComment: IComment;
3032
tags: string[];
3133
tagFilter: any[];
32-
33-
static $inject = ['CommentsService'];
34-
constructor(private CommentsService) {
34+
35+
constructor(private _CommentsService: CommentsService) {
3536
this.emptyComment = {};
3637
this.tagFilter = [];
37-
CommentsService.getComments().then((comments) => {
38+
_CommentsService.getComments().then((comments) => {
3839
this.comments = comments;
3940
this.tags = this.comments
4041
.map((el) => el.tags)

src/decorators.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export function Component(options: {
1212
template?: string,
1313
templateUrl?: string,
1414
bindings?: any,
15+
require?: any,
1516
directives?: any[]
1617
pipes?: any[]
1718
providers?: any[]
@@ -48,6 +49,8 @@ export interface PipeTransform {
4849
export function Pipe(options: {name: string}, moduleOrName: string | ng.IModule = `${appName}.pipes`) {
4950
return (Pipe: PipeTransformStatic) => {
5051
const filter = () => {
52+
console.log(Pipe.$inject);
53+
//@todo: add support for injection across all registered modules
5154
const $injector = angular.injector(['ng']);
5255
const instance:any = $injector.instantiate(Pipe);
5356
return instance.transform.bind(instance);
@@ -56,10 +59,18 @@ export function Pipe(options: {name: string}, moduleOrName: string | ng.IModule
5659
}
5760
}
5861

59-
export function Bootstrap(appName: string, appClass: any) {
62+
63+
export function Injectable() {
64+
return (Class: any) => {
65+
const $injector = angular.injector(['ng']);
66+
Class.$inject = $injector.annotate(Class).map((member) => member.replace(/^_/, ''));
67+
}
68+
}
69+
70+
export function bootstrap(appName: string, appClass: any) {
6071
return (anything: any) => {
6172
if (!appClass) {
62-
console.error(`Please provide main component class as a second argument to @Bootstrap decorator`);
73+
console.error(`Please provide main component class as a second argument to @bootstrap decorator`);
6374
}
6475
angular.element(document).ready(() => {
6576
angular.bootstrap(document, [appName]);

src/main.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import components from './components/components.module';
1010
import pipes from './pipes/pipes.module';
1111
import {appConfig, appName} from "./app.config";
1212
import {AppComponent} from "./components/app.component";
13-
import {Bootstrap} from "./decorators";
13+
import {bootstrap} from "./decorators";
1414

1515
// configure
1616
angular
@@ -25,5 +25,5 @@ angular
2525
.config(appConfig);
2626

2727
// bootstrap
28-
@Bootstrap(appName, AppComponent)
28+
@bootstrap(appName, AppComponent)
2929
class App {}

src/pipes/filterByTags.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,20 @@
22
* Created by voland on 4/2/16.
33
*/
44

5-
import {PipeTransform, Pipe} from '../decorators';
6-
// import {CommentsService} from "../services/comments.service";
5+
import {PipeTransform, Pipe, Injectable} from '../decorators';
6+
import {CommentsService} from "../services/comments.service";
77

8+
@Injectable()
89
@Pipe({name: 'filterByTags'})
910
export class FilterByTagsPipe implements PipeTransform {
10-
static $inject = ['$q'];
11+
// static $inject = ['$q'];
12+
// constructor(private _$q: ng.IQService, private _Comments_Service: CommentsService) {
1113
constructor(private _$q: ng.IQService) {
1214
}
1315

1416
transform(comments: any, tags: any) {
1517
let deferred = this._$q.defer;
16-
// console.log(this._comments.getComments);
18+
// console.log(this._Comments_Service.getComments);
1719
if (!tags.length) return comments;
1820
function check(comment) {
1921
let filterArray = tags.map((tag: any) => tag.text);

0 commit comments

Comments
 (0)