Advertisement
slowkazak

ts-test

Jun 17th, 2016
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //main.ts
  2. import { bootstrap }    from '@angular/platform-browser-dynamic';
  3.  
  4. //инициализируем браузер
  5. import { MainFrontFront} from './frontb2b.front';
  6.  
  7. //инициализируем класс который осуществляет работу приложения
  8. bootstrap(MainFrontFront);
  9. //отправляем в конструктор класса браузера основной класс приложения в качестве параметра
  10.  
  11.  
  12. //MainFront.Front.ts
  13.  
  14. import { Component } from '@angular/core';
  15. import {SearchRes} from "./searchresformat";
  16. import  {SearchService} from "./searchservice";
  17. import {CartFront} from "./cart.front";
  18.  
  19.  
  20. //описываем компонент
  21. @Component({
  22.     selector: 'my-app',
  23.     template: '<h1><ul class="">' +
  24.     '<li *ngFor="let result of _searchres">' +
  25.     '<span class="badge">{{result.cross_name}}</span> {{result.price_cena}}' +
  26.     '' +
  27.     '<button (click)="AddItem(result)">В корзину</button>' +
  28.     '</li>' +
  29.     '</ul>',
  30.     providers: [SearchService, CartFront]
  31. })
  32.  
  33.  
  34.  
  35. export class MainFrontFront{
  36.  
  37.     private _searchres:SearchRes[];
  38. //    private _item:SearchRes;
  39.     public crt
  40.  
  41.     constructor(private _searchservice:SearchService, private cart:CartFront) {
  42.         this._searchres = _searchservice.getRES();
  43.         this.crt = cart
  44.     }
  45.  
  46.     AddItem(item) {
  47.         this.crt.AddCartItem(item)
  48.     }
  49.  
  50. }
  51.  
  52. //searchresformat.ts
  53.  
  54.  
  55. export class SearchRes {
  56.     cross_id:number;
  57.     cross_name:string;
  58.     price_id:number;
  59.     price_cena:number;
  60.     price_count:number;
  61. }
  62.  
  63.  
  64. //cart.front.ts
  65.  
  66. import {Component} from '@angular/core';
  67. import {SearchRes} from "./searchresformat";
  68.  
  69.  
  70. @Component({
  71.     selector: 'cart',
  72.     template: ``
  73.  
  74. })
  75. export class CartFront {
  76.  
  77.   private   cart_items = [];
  78.  
  79.     public GetCart() {
  80.         console.log(this.cart_items);
  81.     }
  82.  
  83.     constructor() {
  84.     }
  85.  
  86.   public  AddCartItem(item) {
  87.     console.log(this.cart_items)
  88.         if (this.cart_items.length < 1) {
  89.             this.cart_items.push(item)
  90.         }
  91.         else
  92.         {
  93.           this.cart_items[0].price_cena = 111;
  94.             console.log(item)
  95.         }
  96.         this.GetCart()
  97.     }
  98.  
  99.  
  100. }
  101.  
  102. //searchservice.ts
  103.  
  104. import  { Injectable } from '@angular/core';
  105. import {SearchRes} from "./searchresformat";
  106. import  {RES} from "./mock-searchres"
  107.  
  108. @Injectable()
  109. export class SearchService {
  110.     getRES() {
  111.  
  112.       return RES;
  113.         //return Promise.resolve(RES);
  114.     }
  115.  
  116.     getRESSlowly() {
  117.         return new Promise<SearchRes[]>(resolve =>
  118.                 setTimeout(()=>resolve(RES), 20)
  119.         );
  120.     }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement