This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Injectable() | |
export class cartService { | |
constructor(private httpclient : HttpClient) {} | |
private cartSubject = new Subject<CartState>(); | |
Products : product[]= []; | |
CartState = this.cartSubject.asObservable(); | |
addProduct(_product:any) { | |
console.log('in service'); | |
this.Products.push(_product) | |
this.cartSubject.next(<CartState>{loaded: true, products: this.Products}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {Injectable} from '@angular/core'; | |
import {Http, Headers, Response, Request, BaseRequestOptions, RequestMethod, ResponseContentType} from '@angular/http'; | |
import {Observable} from 'rxjs/Rx'; | |
@Injectable() | |
export class HttpClient { | |
constructor(private http: Http) {} | |
post(url: string, body: any = {}) { | |
return this.request(url, RequestMethod.Post, body); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Add a new comment | |
addComment (body: Object): Observable<Comment[]> { | |
let bodyString = JSON.stringify(body); // Stringify payload | |
let headers = new Headers({ 'Content-Type': 'application/json' }); // ... Set content type to JSON | |
let options = new RequestOptions({ headers: headers }); // Create a request option | |
return this.http.post(this.commentsUrl, body, options) // ...using post request | |
.map((res:Response) => res.json()) // ...and calling .json() on the response to return data | |
.catch((error:any) => Observable.throw(error.json().error || 'Server error')); //...errors if any | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Injectable() | |
export class CommentService { | |
// Resolve HTTP using the constructor | |
constructor (private http: Http) {} | |
// private instance variable to hold base url | |
private commentsUrl = 'http://localhost:3000/api/comments'; | |
} | |
getComments() : Observable<Comment[]> { | |
// ...using get request | |
return this.http.get(this.commentsUrl) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
request(url: string | Request, options?: RequestOptionsArgs): Observable<Response>; | |
/** | |
* Performs a request with `get` http method. | |
*/ | |
get(url: string, options?: RequestOptionsArgs): Observable<Response>; | |
/** | |
* Performs a request with `post` http method. | |
*/ | |
post(url: string, body: any, options?: RequestOptionsArgs): Observable<Response>; | |
/** |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Component({ | |
selector: 'video-list', | |
templateUrl: 'video-list.html' | |
}) | |
class VideoList implements OnInit { | |
@Input() Videos:video[] ; | |
@Output() clickParentChange = new EventEmitter(); | |
ngOnInit(){ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Component({selector: 'youtube', templateUrl: './youtube.html'}) | |
export default class Youtube implements OnInit { | |
Videos : video[]; | |
loaded : boolean; | |
API_KEY : string; | |
selectVideo : video; | |
opts : any; | |
constructor() { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Component, Input, OnInit, Output, EventEmitter } from '@angular/core'; | |
@Component | |
({ | |
selector: 'search-bar', | |
template: '<div class="search-bar" ><input [(ngModel)]="searchText" (change)="handleChange()" /></div>' . | |
}) | |
export default class SearchBar implements OnInit { | |
@Output() searchTextChange = new EventEmitter(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
render() { | |
const { location, pattern, match, isExact } = this.props | |
return ( | |
<div> | |
<h1>Reading the query parameters.</h1> | |
<div className="leftNavi"> | |
<ul> | |
<li><Link to={{ | |
pathname: match.url+'/level1', | |
search: '?abc=23' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default class BasicRouting extends Component { | |
render() { | |
return ( | |
<div> | |
<h1>BasicRouting</h1> | |
<div className="leftNavi"> | |
<ul> | |
<li><Link to={this.props.match.url +"/level1"} className="active">Level 1</Link></li> | |
<li><Link to={this.props.match.url + "/level2"} className="active">Level 2</Link></li> | |
</ul> |