Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //main.ts
- import { bootstrap } from '@angular/platform-browser-dynamic';
- //инициализируем браузер
- import { MainFrontFront} from './frontb2b.front';
- //инициализируем класс который осуществляет работу приложения
- bootstrap(MainFrontFront);
- //отправляем в конструктор класса браузера основной класс приложения в качестве параметра
- //MainFront.Front.ts
- import { Component } from '@angular/core';
- import {SearchRes} from "./searchresformat";
- import {SearchService} from "./searchservice";
- import {CartFront} from "./cart.front";
- //описываем компонент
- @Component({
- selector: 'my-app',
- template: '<h1><ul class="">' +
- '<li *ngFor="let result of _searchres">' +
- '<span class="badge">{{result.cross_name}}</span> {{result.price_cena}}' +
- '' +
- '<button (click)="AddItem(result)">В корзину</button>' +
- '</li>' +
- '</ul>',
- providers: [SearchService, CartFront]
- })
- export class MainFrontFront{
- private _searchres:SearchRes[];
- // private _item:SearchRes;
- public crt
- constructor(private _searchservice:SearchService, private cart:CartFront) {
- this._searchres = _searchservice.getRES();
- this.crt = cart
- }
- AddItem(item) {
- this.crt.AddCartItem(item)
- }
- }
- //searchresformat.ts
- export class SearchRes {
- cross_id:number;
- cross_name:string;
- price_id:number;
- price_cena:number;
- price_count:number;
- }
- //cart.front.ts
- import {Component} from '@angular/core';
- import {SearchRes} from "./searchresformat";
- @Component({
- selector: 'cart',
- template: ``
- })
- export class CartFront {
- private cart_items = [];
- public GetCart() {
- console.log(this.cart_items);
- }
- constructor() {
- }
- public AddCartItem(item) {
- console.log(this.cart_items)
- if (this.cart_items.length < 1) {
- this.cart_items.push(item)
- }
- else
- {
- this.cart_items[0].price_cena = 111;
- console.log(item)
- }
- this.GetCart()
- }
- }
- //searchservice.ts
- import { Injectable } from '@angular/core';
- import {SearchRes} from "./searchresformat";
- import {RES} from "./mock-searchres"
- @Injectable()
- export class SearchService {
- getRES() {
- return RES;
- //return Promise.resolve(RES);
- }
- getRESSlowly() {
- return new Promise<SearchRes[]>(resolve =>
- setTimeout(()=>resolve(RES), 20)
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement