成人视屏在线观看-国产99精品-国产精品1区2区-欧美一级在线观看-国产一区二区日韩-色九九九

您的位置:首頁技術文章
文章詳情頁

javascript - Angular2中,組件傳參問題

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

問題描述

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

我想要的效果就如上圖所示,現在我已經實現了紅色框內的組件了,而且也實現了紅色框中點擊增加數字的效果。但是購物車中,我卻無法實現總量的計算。請大牛指點下吧!相關代碼如下:

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://m.cgvv.com.cn/wenda/12558.html'>蒸點類</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://m.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://m.cgvv.com.cn/assets/images/none.jpg' alt=''> </span> </p></li> </ul> </p> <cart [count]='cartcount'></cart> </p></ion-content>

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

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(購物車組件)

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://m.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>

問題解答

回答1:

本人自己已經解決了問題,下面貼出方法,讓關注同一問題的朋友也能知道答案。在segmentfault上發了兩次提問,都沒有人回答。好無奈的!解決方法如下:引入AfterViewInit、ViewChildren和QueryList

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

標簽: JavaScript
相關文章:
主站蜘蛛池模板: 视频一区色眯眯视频在线 | 黄网站免费在线 | 亚洲男人a天堂在线2184 | 武松大战潘金莲三级在线 | 国产一级毛片午夜 | 久久精品国产免费观看99 | 玖玖精品视频在线观看 | 特黄毛片| 免费高清欧美一区二区视频 | 久久精品网站免费观看 | 国产成人综合久久亚洲精品 | 久久99热成人精品国产 | 精品一区二区三区在线观看 | 国产成人偷拍 | 911精品国产91久久久久 | 免费萌白酱国产一区二区三区 | 国产女人自拍 | 最近中文字幕在线 | 中文 | 免费的一级片网站 | 爆操巨乳美女 | 国产亚洲自在精品久久 | 天堂资源8中文最新版在线 天堂最新版 | 欧美亚洲一级片 | 国产精品久久久久免费 | 欧美一级免费 | 秘书高跟黑色丝袜国产91在线 | 美国一级毛片a | 精品久久免费观看 | 亚洲超大尺度激情啪啪人体 | 狠狠色噜噜狠狠狠米奇9999 | 日韩一区二区视频在线观看 | 国产成人久久精品 | 99精品高清视频一区二区 | av国产精品 | 黄色18网站| 夜色爽爽| 九九视频在线播放 | 国产精品日韩欧美在线第3页 | 日本精品一区二区三区在线 | 国产成人精品高清不卡在线 | 日本免费特黄aa毛片 |