Skip to content

Instantly share code, notes, and snippets.

View tkssharma's full-sized avatar
🎯
only JS

codewithtkssharma tkssharma

🎯
only JS
View GitHub Profile
@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});
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);
}
// 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
}
@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)
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>;
/**
@Component({
selector: 'video-list',
templateUrl: 'video-list.html'
})
class VideoList implements OnInit {
@Input() Videos:video[] ;
@Output() clickParentChange = new EventEmitter();
ngOnInit(){
@Component({selector: 'youtube', templateUrl: './youtube.html'})
export default class Youtube implements OnInit {
Videos : video[];
loaded : boolean;
API_KEY : string;
selectVideo : video;
opts : any;
constructor() {
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();
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'
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>