代码之家  ›  专栏  ›  技术社区  ›  Rohit B

角度:使用服务将单个列表项从数组列表传输到另一个服务?

  •  0
  • Rohit B  · 技术社区  · 8 年前

    将单个项目从列表项目转移到购物车列表。

    我正在开发 有棱角的 web应用程序,并希望当我单击按钮时,数组的单个项从一个 服务 传输到另一个服务,并在另一个组件上传输。我已经成功地实现了整个阵列的传输,但我面临着单个项目的问题。请帮忙。


    我想要的是当我点击 Add to cart 按钮仅传输单击的列表项,而不传输列表项数组。


    buyGame。html文件

    <div class="col-xs-6">
        <a class="list-group-item clearfix" style="background-color:rgb(3, 0, 48)" *ngFor="let buying of buy">
            <div class="pull-left" style="max-width:330px">
                <h5 style="color:white">{{buying.names}}</h5>
                <p style="color:white">{{buying.desc}}</p>
                <button class="btn btn-danger ; pull-left" (click)= "onAddToCart()">Add To Cart</button>
            </div>
            <div>
                <span class="pull-right">
                    <img [src]="buying.getImg" alt="image not loaded" class="img-responsive" style="max-height:100px">
                </span>
            </div>
        </a>
    </div>
    

    buygame。服务ts文件:

    import { gameBuy } from "./buygame.model";
    import { Injectable,EventEmitter } from "@angular/core";
    import { cartService } from "./cart.service";
    
    @Injectable()
    export class gameService{
    
        private gameServ: gameBuy[] = [
            new gameBuy('batman','Batmobile and enhancements to signature features',"https://www.geek.com/wp-content/uploads/2016/02/batmans-625x352.jpg"),
            new gameBuy('GTA 5',
            "PlayStation 3 or Xbox 360 will be able to transfer their current Grand Theft Auto Online characters and progression to their choice of PlayStation 4 Xbox One or PC",
            "http://onlysp.com/wp-content/uploads/2015/01/maxresdefault.jpg")
        ];
    
        constructor(private cartSer: cartService){}
    
        getBuyingList(){
            return this.gameServ.slice();
        }
    
        addItemToCart(game:gameBuy[]){
            this.cartSer.addItem(game);
        }
    }
    

    buyGame。组成部分ts:

    import { Component, OnInit,Input } from '@angular/core';
    import { gameBuy } from '../shared/buygame.model';
    import { gameService } from '../shared/buygame.service';
    
    @Component({
      selector: 'app-buy-game',
      templateUrl: './buy-game.component.html',
      styleUrls: ['./buy-game.component.css'],
    })
    export class BuyGameComponent implements OnInit {
    
      @Input() buy:gameBuy[];
    
      constructor(private service: gameService) { }
    
      ngOnInit() {
        this.buy = this.service.getBuyingList();
      }
    
      onAddToCart(){
     this.service.addItemToCart(this.buy);
      }
    }
    

    运货马车组成部分ts:

    import { Component, OnInit} from '@angular/core';
    import { cartModel } from '../shared/cart.model';
    import { cartService } from '../shared/cart.service';
    import { gameBuy } from '../shared/buygame.model';
    
    @Component({
      selector: 'app-cart',
      templateUrl: './cart.component.html',
      styleUrls: ['./cart.component.css'],
    })
    export class CartComponent implements OnInit {
    
       cart:gameBuy[];
    
      constructor(private service: cartService) { }
    
      ngOnInit() {
        this.cart = this.service.getCartItem();
      }
    
    }
    

    运货马车服务ts:

    import { cartModel } from "./cart.model";
    import { EventEmitter } from "@angular/core";
    import { gameBuy } from "./buygame.model";
    
    export class cartService{
    
        cartChanged = new EventEmitter<gameBuy[]>();
        private cart: gameBuy[] = [
            new gameBuy('Batman','Batman is a cool game','https://images-na.ssl-images-amazon.com/images/I/91lu5KHSm3L._SY445_.jpg'),
            new gameBuy('Gta 5','online game of GTA','https://www.rockstargames.com/V/img/global/order/mobile-cover.jpg')
        ];
    
        getCartItem(){
            return this.cart.slice();
        }
    
        addItem(cart:gameBuy[]){
            this.cart.push(...cart);
            this.cartChanged.emit(this.cart.slice());
        }
    }
    

    运货马车模型ts:

    export class cartModel{
        constructor(public cartName: string,public cartDesc: string,public cartImage:string){}
    }
    

    buygame。模型ts:

    export class gameBuy{
        constructor(public names:string, public desc:string, public getImg:string){}
    }
    
    2 回复  |  直到 8 年前
        1
  •  3
  •   dhilt    8 年前

    您需要在模板中指定要添加到购物车的确切项目:

    (click)= "onAddToCart(buying)"
    

    然后将其作为 onAddToCart 方法参数:

    onAddToCart(buying: gameBuy){
      this.service.addItemToCart(buying);
    }
    

    此外,您的 buygame 服务方法应接受单个项目,而不是列表:

    addItemToCart(game: gameBuy){
        this.cartSer.addItem(game);
    }
    

    Atl last, cart 服务也应该更新(只是为了推送单个项目)

     addItem(cart:gameBuy){
        this.cart.push(cart);
        this.cartChanged.emit([...this.cart]); // slice() is ok too if you need a copy
    }
    
        2
  •  0
  •   Rohit.007    8 年前

    尝试在调用中提供索引(单击)=“onAddToCart(index)”并从数组中获取它。

    或者在(单击)=“onAddToCart(buying)”中提供单个对象

    然后在TS上接收

    推荐文章