angular - 如果创建了按钮,角度添加计数器++?
问题描述
我的 Angular 按钮在单击时应该添加一个新按钮,按钮应该count++
是 ,因此每次单击按钮时,按钮内的数字都应添加到 + 1。
这里的主要问题是,我尝试使用服务将一个数字从一个组件发送到另一个组件,并且每次单击该按钮时,它都应该添加一个带有该新数字的新按钮。
我希望你们能帮助我,这是我到目前为止得到的:
app.component.html
<div>
<h2>By click on plus there should be added a new button with a new number</h2>
<div class="item" *ngFor="let item of obj" (click)="addButton()">
<span>{{item.id}}</span>
<span>{{item['count'] ? item['count'] : item['count'] = 0}}</span>
<button (click)="increment(item)">plus</button>
<button (click)="decrement(item)">minus</button>
</div>
</div>
<app-dialog></app-dialog>
<p>Add Button with different number</p>
<h2>{{counter}}</h2>
<div *ngFor="let button of buttons">
<button (click)="addButton()">{{counter}}</button>
</div>
app.component.ts
import { isNgTemplate } from '@angular/compiler';
import { Component, OnInit } from '@angular/core';
import { Subscription } from 'rxjs';
import { ShareDataService } from './share-data.service'
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent {
clickEventsubscription: Subscription
id: number;
title:String;
obj = [{ id: 0, }, { title: 'test' }];
constructor(private share: ShareDataService) {
this.clickEventsubscription = this.share.getClickEvent().subscribe(() => {
this.incrementCount(this.counter);
this.addButton();
})
}
ngOnInit() {
}
counter: number = 0;
incrementCount(counter: any) {
this.counter++;
}
increment(item) {
item.count++;
}
decrement(item) {
item.count--;
}
buttons : {
id : number,
}[] = [{ id : 0}];
// Function to add new button
addButton(){
this.buttons.push({
id : this.buttons.length,
})
}
}
这是 dialog.component.html:
<ol>
<li>
<button mat-raised-button (click)="openDialog()">Pick one</button>
</li>
<li *ngIf="animal">
You chose: <i>{{animal}}</i>
</li>
</ol>
<button (click)="clickMe()">Add id 1</button>
对话框.component.ts:
import { Component, Inject, OnInit } from '@angular/core';
import {MatDialog, MatDialogRef, MAT_DIALOG_DATA} from '@angular/material/dialog';
import { ShareDataService } from './../share-data.service';
@Component({
selector: 'app-dialog',
templateUrl: './dialog.component.html',
styleUrls: ['./dialog.component.scss']
})
export class DialogComponent implements OnInit {
animal: string;
name: string;
constructor(public dialog: MatDialog, private share: ShareDataService) {}
ngOnInit(): void {
throw new Error('Method not implemented.');
}
openDialog(): void {
const dialogRef = this.dialog.open(DialogComponent, {
width: '250px',
data: {name: this.name, animal: this.animal}
});
dialogRef.afterClosed().subscribe(result => {
console.log('The dialog was closed');
this.animal = result;
});
}
clickMe(){
this.share.sendClickEvent();
}
最后我的服务:
import { Injectable } from '@angular/core';
import { Observable, Subject } from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class ShareDataService {
private subject = new Subject<any>();
sendClickEvent(){
this.subject.next();
}
getClickEvent() : Observable<any>{
return this.subject.asObservable();
}
}
谢谢!
解决方案
推荐阅读
- asp.net - ASP.NET 重定向 URL 仅在 / 之后有某些内容时
- angular - 如何在 Angular 中为 Karma/jasmine 测试创建存根
- typescript - 如果存在另一个道具,则完全包含 Typescript 界面道具
- c# - ComboBox 设置一个值 C#
- python - 如何打开文件夹中的第一个文件
- flutter - Flutter:如何将base64转换为图像?
- android - 如何在android中使用OkHttp从web api获取json数据到RecyclerView
- android - 当应用程序在后台或终止时,iOS 设备不会推送通知。导航到下一个屏幕在 Flutter 的 IOS 设备中也不起作用?
- angular - 我尝试将数据从我的组件添加到其他组件
- azure-container-instances - Azure 容器实例在启动时立即终止