Who the hell is this guy?
Front-end Architect
Angular GDE
videogular
import {Component, ViewEncapsulation, CORE_DIRECTIVES} from 'angular2/angular2';
@Component({
selector: 'github',
templateUrl: './components/github/github.html',
styleUrls: ['./components/github/github.css'],
encapsulation: ViewEncapsulation.Emulated,
directives: [CORE_DIRECTIVES]
})
export class GithubCmp {
public projects: Array;
constructor() {
this.projects = [];
}
onSearch(search) {
console.log("Search projects on Github by keyword " + search);
}
}
github.ts
<form (submit)="onSearch(search)">
<input type="text" #search>
<button type="submit">Search</button>
</form>
<div *ng-for="#project of projects"
class="project">
<h3>{{ project.name }}</h3>
<h4>{{ project.stargazers_count }}</h4>
<p>{{ project.description }}</p>
</div>
github.html
/*
We can emulate :host selector thanks to
'encapsulation: ViewEncapsulation.Emulated'
*/
:host .project h3, :host .project h4 {
display: inline-block;
}
:host .project h4:before {
content: url('components/github/star.svg');
margin-left: 10px;
vertical-align: sub;
}
github.css
import {bootstrap, provide} from 'angular2/angular2';
import {ROUTER_PROVIDERS, APP_BASE_HREF, LocationStrategy, HashLocationStrategy} from 'angular2/router';
import {HTTP_PROVIDERS} from 'angular2/http';
import {AppCmp} from './components/app/app';
bootstrap(AppCmp, [
provide(APP_BASE_HREF, { useValue: '<%= APP_BASE + APP_DEST %>' } ),
ROUTER_PROVIDERS,
HTTP_PROVIDERS,
provide(LocationStrategy, { useClass: HashLocationStrategy })
]);
bootstrap.ts
import {Injectable} from 'angular2/angular2';
import {Http} from 'angular2/http';
import Observable from '../../node_modules/@reactivex/rxjs/src/Observable';
import {Response} from '../../node_modules/angular2/ts/src/http/static_response';
@Injectable()
export class GithubService {
url: string = 'https://api.github.com/search/repositories?q=';
http: Http;
constructor(http: Http) {
this.http = http;
}
search(query: string): Observable<Response> {
return this.http.get(this.url + query);
}
}
github_service.ts
import {Component, ViewEncapsulation, CORE_DIRECTIVES} from 'angular2/angular2';
import {GithubService} from '../../services/github_service';
@Component({
selector: 'github',
templateUrl: './components/github/github.html',
styleUrls: ['./components/github/github.css'],
encapsulation: ViewEncapsulation.Emulated,
directives: [CORE_DIRECTIVES]
})
export class GithubCmp {
public projects: Array;
constructor(public ghService: GithubService) {
this.projects = [];
}
onSearch(search) {
this.ghService
.search(search.value)
.map(res => res.json().items)
.map(projects => projects.sort((a, b) => (b.stargazers_count - a.stargazers_count)))
.subscribe(projects => this.projects = projects);
}
}
github.ts
import {Component, ViewEncapsulation} from 'angular2/angular2';
import {
RouteConfig,
ROUTER_DIRECTIVES
} from 'angular2/router';
import {HomeCmp} from '../home/home';
import {AboutCmp} from '../about/about';
import {GithubCmp} from '../github/github';
import {NameList} from '../../services/name_list';
import {GithubService} from '../../services/github_service';
@Component({
selector: 'app',
viewProviders: [NameList, GithubService],
templateUrl: './components/app/app.html',
styleUrls: ['./components/app/app.css'],
encapsulation: ViewEncapsulation.None,
directives: [ROUTER_DIRECTIVES]
})
@RouteConfig([
{ path: '/', component: HomeCmp, as: 'Home' },
{ path: '/about', component: AboutCmp, as: 'About' },
{ path: '/github', component: GithubCmp, as: 'Github' }
])
export class AppCmp {}
app.ts
<section class="sample-app-content">
<nav>
<a [router-link]="['/Home']">Home</a>
<a [router-link]="['/About']">About</a>
<a [router-link]="['/Github']">Github</a>
</nav>
<router-outlet></router-outlet>
</section>
app.html