国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

javascript - Angular2中,組件傳參問(wèn)題

瀏覽:101日期:2024-02-24 16:15:15

問(wèn)題描述

目前我有三個(gè)組件,父組件下ngFor中有一個(gè)子組件,因此循環(huán)出來(lái)的就有好幾個(gè)子組件,而每個(gè)子組件的內(nèi)容可能都不一樣。那么我要如何將此子組件中所有的內(nèi)容獲取?,如下圖,請(qǐng)使用angular2javascript - Angular2中,組件傳參問(wèn)題

我想要的效果就如上圖所示,現(xiàn)在我已經(jīng)實(shí)現(xiàn)了紅色框內(nèi)的組件了,而且也實(shí)現(xiàn)了紅色框中點(diǎn)擊增加數(shù)字的效果。但是購(gòu)物車(chē)中,我卻無(wú)法實(shí)現(xiàn)總量的計(jì)算。請(qǐng)大牛指點(diǎn)下吧!相關(guān)代碼如下:

foodinfo.ts

import {Component, Injectable, OnInit, ElementRef} from ’@angular/core’;import { NavController,NavParams } from ’ionic-angular’;import ’rxjs/Rx’;import { Http } from ’@angular/http’;import {food} from ’../../app/food’;import {Count} from ’../count/count’;import {Cart} from ’../cart/cart’;@Component({ selector: ’page-foodinfo’, templateUrl: ’foodinfo.html’, providers:[Count,Cart]})@Injectable()export class FoodInfo implements OnInit{ public title =’’; public Foods :food[]; public apiUrl=’http://www.egtch.com/demo/bindo/data.php’; public id=0; public sid = 0; public sel =[]; public show= true; public cartcount = []; public iscount:number; public price:number; public itemPrice:number; constructor(public navCtrl: NavController,navParams:NavParams, public http: Http,el:ElementRef) { this.id = navParams.get(’isId’) - 1; this.title = navParams.get(’title’); http.get(this.apiUrl).subscribe(res=> this.Foods = res.json()[this.id].cat); http.get(this.apiUrl).subscribe(res=> this.sel = res.json()[this.id].cat[this.sid].setmeal); var ele = el.nativeElement.querySelector(’.count’); console.log(ele); } /*@ViewChild(Count) count:Count;*/ /*ngAfterViewInit() { // do something with list items console.log(this.count); }*/ ngOnInit(){} getSid(sid){ this.sid=sid; console.log(sid); this.http.get(this.apiUrl).subscribe(res=> this.sel = res.json()[this.id].cat[this.sid].setmeal); } getCount(event){ this.cartcount = event; //for(let i=0;){} //console.log(this.cartcount); }}

foodinfo.html

<ion-header> <ion-navbar> <ion-title> {{title}} </ion-title> </ion-navbar></ion-header><ion-content> <p class='goods'> <p *ngIf='Foods'> <!--<a href='http://www.cgvv.com.cn/wenda/12558.html'>蒸點(diǎn)類(lèi)</a>--> <a *ngFor='let l of Foods;let i=index' (click)='getSid(i)'>{{l.name}}</a> </p> <p class='list-wrapper'> <p class='pic'><img src='http://www.cgvv.com.cn/assets/images/food_big.png'></p> <ul><li *ngFor='let s of sel;let o = index'> <span class='title'>{{s.name}}({{s.numb}}件)</span> <p class='info'> <span class='price'>${{s.price}}</span> <span *ngIf='s.numb>0'><count (countOut)='getCount($event)'></count> </span> <span *ngIf='s.numb<=0'><img src='http://www.cgvv.com.cn/assets/images/none.jpg' alt=''> </span> </p></li> </ul> </p> <cart [count]='cartcount'></cart> </p></ion-content>

count.ts(這個(gè)就是紅色框的組件)

import { Component, Output, EventEmitter } from ’@angular/core’;import { NavController } from ’ionic-angular’;@Component({ selector: ’count’, templateUrl: ’count.html’})export class Count { public show= true; public icount=0; constructor(public navCtrl: NavController) {}; addCount(){ this.icount++; this.show = false; console.log(this.icount); this.countOut.emit(this.icount); } delCount(){ if(this.icount>0){ this.icount--; this.show = false; } if(this.icount<1){ this.icount = 0; this.show = true; } this.countOut.emit(this.icount); } @Output() countOut = new EventEmitter();}

count.html

<a [hidden]='show' (click)='delCount()'>-</a><a [hidden]='show'>{{icount}}</a><a (click)='addCount()'>+</a>

cart.ts(購(gòu)物車(chē)組件)

import { Component,Input } from ’@angular/core’;import { NavController } from ’ionic-angular’;@Component({ selector: ’cart’, templateUrl: ’cart.html’})export class Cart { constructor(public navCtrl: NavController) {}; @Input() count: number; @Input() selectFood; totalCount(v) { let count = 0; count = v; //console.log(this.count) return count; }}

cart.html

<p class='cart'> <p class='cart-item'><img src='http://www.cgvv.com.cn/assets/images/cart.png'><span *ngIf='totalCount(count)>0'>{{totalCount(count)}}</span></p> <p class='cart-order'>查看訂單</p> <p class='cart-pay'>$220</p></p>

問(wèn)題解答

回答1:

本人自己已經(jīng)解決了問(wèn)題,下面貼出方法,讓關(guān)注同一問(wèn)題的朋友也能知道答案。在segmentfault上發(fā)了兩次提問(wèn),都沒(méi)有人回答。好無(wú)奈的!解決方法如下:引入AfterViewInit、ViewChildren和QueryList

import {Component, AfterViewInit, QueryList, ViewChildren} from ’@angular/core’;export class FoodInfo implements AfterViewInit{}ngAfterViewInit(){}//獲得子組件的集合@ViewChildren(Count) listCount: QueryList<Count>;

標(biāo)簽: JavaScript
相關(guān)文章:
主站蜘蛛池模板: 美女黄频免费观看 | 国产精品久久久久免费 | 成年大片免费视频播放手机不卡 | 欧美在线观看一区二区三区 | 久久伊人精品热在75 | 国产精品二区在线 | 成人免费影视网站 | 男女乱淫视频 | 亚洲欧美日韩在线一区 | 欧洲成人免费视频 | 午夜性爽爽爽 | 国产福利三区 | 亚洲视频在线观看一区 | 亚洲欧洲日本天天堂在线观看 | 欧美日韩一区二区三区高清不卡 | 尹人在线视频 | 国产成人精品在视频 | 91资源在线观看 | 亚洲欧美另类色妞网站 | 免费播放aa在线视频成人 | 草草影院欧美三级日本 | 亚洲欧美一区二区三区 | freex性日韩 free性chinese国语对白 | 91久久亚洲国产成人精品性色 | 久久777国产线看是看精品 | 国内精品久久久久久久星辰影视 | 天空在线观看免费完整 | 亚洲综合视频网 | 美女曰皮| 91精品国产手机 | 欧美高清一区二区三区欧美 | 日本一区二区三区四区无限 | 99在线精品免费视频九九视 | 午夜私人影院免费体验区 | 巨大热杵在腿间进进出出视频 | 91福利精品老师国产自产在线 | 日本天堂视频在线观看 | 免费观看成年的网站 | 精品在线播放视频 | 午夜精品网 | 日本一区三区二区三区四区 |