angular - Angular ngFor中的组件@Input
问题描述
我有以下代码来生成卡片。父组件.html:
<div *ngFor="let goal of goalList" class="row col-8 my-3 offset-2">
<div class="card" style="width: 100rem;">
<div class="card-header">
<div class="row">
<app-comment class="col-1 text-left align-middle" [ID]=goal.id></app-comment>
<div class="col-11 text-center text-center">{{'Goals.Goal'|translate}}</div>
</div>
</div>
<div class="card-body">
<div class="row">
<div class="col-11 card-text">{{goal.text}}</div>
</div>
<div class="row">
<mat-slider thumbLabel tickInterval="1" max="5" step="0.5" class="col-10 offset-1"
[(ngModel)]="goal.EMPrating" (ngModelChange)="SaveGoalRating(goal)" color="primary"></mat-slider>
</div>
<div class="row">
<mat-form-field class="col-4 offset-4">
<input name="ActualDate" matInput [matDatepicker]="dtpicker" placeholder="{{'Goals.ActualDate'|translate}}"
[(ngModel)]="goal.ActualDate" (ngModelChange)="SaveGoalDate(goal)">
<mat-datepicker-toggle matSuffix [for]="dtpicker"></mat-datepicker-toggle>
<mat-datepicker #dtpicker></mat-datepicker>
</mat-form-field>
</div>
</div>
</div>
</div>
parent.component.ts中获取goalList的代码
LoadGoals(){
this.goalList = [];
let goalArray: string[] = [];
this.goalService.GetGoalResults(this.cookieService.get('email'),this.SurveyID).subscribe(result=>{
result.split('\n').map(row=>{
if (row != ""){
goalArray = row.split(';');
let d: Date = new Date (goalArray[3]);
this.goalList.push({id:goalArray[0],text:goalArray[1],targetDate:null,ActualDate:d, status:null,EMPrating:Number(goalArray[2])});
}
})
});
}
目标等级:
export class Goal {
id:string;
text:string;
targetDate:string;
ActualDate:Date;
status:string;
}
评论.component.html:
<div>
<button mat-icon-button data-toggle="modal" data-target="#CommentModal" color="primary">
<i class="material-icons">question_answer</i>
</button>
<div class="modal fade" id="CommentModal" tabindex="-1" role="dialog" aria-labelledby="CommentTitle" aria-hidden="true">
<div class="modal-dialog modal-lg" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="CommentTitle">Comments</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div class="row mt-3">
<textarea class="form-control col-10 offset-1" [(ngModel)]="CommentText"></textarea>
</div>
<div class="row my-3">
<button class="btn btn-success oi oi-check col-2 offset-5" [disabled]="!HasComment()" (click)="SubmitComment()"></button>
</div>
<div class="row">
<span class="offset-1">Here will be comments.</span>
</div>
</div>
</div>
</div>
</div>
</div>
评论.component.ts:
import { Component, OnInit, Input } from '@angular/core';
import { CookieService } from 'ngx-cookie-service';
import { CommentService } from '../comment.service';
@Component({
selector: 'app-comment',
templateUrl: './comment.component.html',
styleUrls: ['./comment.component.css']
})
export class CommentComponent implements OnInit {
@Input() ID:string;
private CommentText: string = "";
constructor(private cookieService: CookieService, private commentService: CommentService) {
}
ngOnInit() {
}
SubmitComment(){
alert(this.ID);
//this.commentService.Submit("",this.cookieService.get('email'),this.CommentText);
}
HasComment():boolean{
return this.CommentText != "";
}
}
检查生成的 html 后,它看起来不错。第一个应用评论包含
ng-reflect--i-d="10"
而第二个有
ng-reflect--i-d="1010"
当 SubmitComment() 运行以提醒 ID 输入时,它始终显示第一个目标 (10) 的 ID。如何将卡的 ID (goal.id) 传递给其中的应用评论?
打字稿部分肯定没问题。我添加了
<span>{{ID}}</span>
到 comment.component.html 并显示正确的 ID。问题必须围绕html。
提前致谢!
解决方案
在一些评论之后,解决方案是动态改变 ID[attr.data-target]="'#CommentModal'+ID"
并且 [ id]="'CommentModal'+ID"
Bootstrap 获取第一个 id“CommentModal”所以这就是为什么它总是返回第一个。
推荐阅读
- linux - 如何在 Oracle Linux Server 6.8 上更改没有名称的时区?
- excel - 如何在excel或VBA中搜索带有多个“-”的项目?
- python - 如何使用 attrs 定义数组对象?
- android - Jitpack 存储库检索到错误版本的依赖项
- mysql - 在mysql中的列中查找逗号分隔字符串中第一个非负整数的索引
- python - 使用 LU 分解实现 Ax = b 求解器时遇到问题
- c# - 带有引导程序和图像的 Foreach 列出 6 个列项
- django - 如何在 Django 中过滤特定用户的帖子?
- android - 无法确定 com.github.Kunzisoft:Android-SwitchDateTimePicker:1.9 的工件:由于早期错误而跳过
- java - Web 服务中是否有类似设施的监听器?