javascript - 如何以角度复制div
问题描述
我正在使用 innerHtml 以角度显示来自变量的 html 内容,但它不支持外部样式和 ngFor 。每当用户单击添加服务按钮时,在下面的屏幕中,一行应该增加。 这是服务列表中的数据。
<div class="col-sm-6 col-md-4">
<label class="mobileNumberLabel " for="mobilrNumber">Select Service</label>
<div class="nice-wrap">
<select class="js-example-basic-single nice-textbox" style="width: 105%">
<optgroup *ngFor="let item of serviceObject" label='{{item.categoryName}}'>
<option *ngFor="let service of item.services">
{{service.serviceName}} - <span >{{item.categoryName}}</span>
</option>
</optgroup>
</select></div>
</div>
我试过 innerHtml
<div [innerHTML]="divItem"></div>
解决方案
我认为您想将响应式表单动态添加到 HTML 中。
前段时间我遇到过这种情况,因为我不得不动态添加新段落。
在这段代码中,我有一个文本区域作为段落的输入。我可以通过单击按钮添加段落并通过单击Add New Paragraph
按钮删除段落Remove
。
.html 文件应该有 -
<div formArrayName="contentDescriptions">
<div *ngFor="let contentItem of contentDescriptions.controls; let i=index">
<textarea [formControlName]="i" name="" id="" cols="30" rows="6" placeholder="Enter Description"></textarea>
<button (click)="deleteContentDescription(i)"> Remove</button>
</div>
<div>
<button (click)="addNewParagraph()">
<span class="icon icon-plus"></span> Add New Paragraph
</button>
</div>
</div>
.ts 文件应该有 -
form = this.fb.group({
contentDescriptions: this.fb.array([
this.fb.control('')
])
});
get contentDescriptions() {
return this.form.get('contentDescriptions') as FormArray;
};
constructor(private fb: FormBuilder) { }
ngOnInit(){
this.form.patchValue({
contentDescriptions: this.contentSelected.descriptionArray
});
}
addNewParagraph(){
this.contentDescriptions.push(this.fb.control(''));
}
deleteContentDescription(i){
this.contentDescriptions.removeAt(i);
}
推荐阅读
- mongodb - 选择数组的所有对象都具有某些值的文档
- php - PHP password_hash 和 password_verify 奇怪的问题没有验证
- android-studio - 我如何使 android studio 代码不是一行
- python - 在不关闭 Flask 服务器的情况下运行多个功能
- c - Clang 参数 -Wno-conditional-type-mismatch 和 GCC
- html - Rails Ransack 没有正确过滤但对象状态?
- python - 可以调整 Ardunio 代码以从 Spyder(或其他 Python IDE)运行吗?
- python - 用ansible从csv文件中读取变量名并用值替换
- apache-spark - PySpark:检查一列的值是否与下一行的另一列匹配
- php - 如何从数据库中查看图像到网站文档?