Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.78 KB | None | 0 0
  1. import {Component, OnInit} from '@angular/core';
  2. import {FormGroup, FormArray, FormControl} from '@angular/forms';
  3.  
  4. @Component({
  5. selector: 'app-services-cu',
  6. templateUrl: '../pages/fileuploader-cu.component.html'
  7. })
  8.  
  9. export class FileuploaderCuComponent implements OnInit {
  10. uploader: FormGroup;
  11.  
  12. constructor() {
  13. }
  14.  
  15. ngOnInit() {
  16. this.uploader = new FormGroup({
  17. sections: new FormArray([
  18. this.initSection(),
  19. ]),
  20. });
  21. }
  22.  
  23. initSection() {
  24. return new FormGroup({
  25. beforeImage: new FormControl(''),
  26. afterImage: new FormControl('')
  27. });
  28. }
  29.  
  30. addSection() {
  31. const control = <FormArray> this.uploader.get('sections');
  32. control.push(this.initSection());
  33. }
  34.  
  35. getSections(form) {
  36. return form.controls.sections.controls;
  37. }
  38.  
  39. removeSection(i) {
  40. const control = <FormArray> this.uploader.get('sections');
  41. control.removeAt(i);
  42. }
  43.  
  44. onSubmit(form) {
  45. console.log(this.uploader);
  46. }
  47. }
  48.  
  49. <div class="row">
  50. <div class="col-md-12 col-sm-12 col-xs-12">
  51. <div class="x_panel">
  52. <form [formGroup]="uploader" novalidate (ngSubmit)="onSubmit(uploader)">
  53. <!--- Section -->
  54. <div class="row">
  55. <div formArrayName="sections">
  56. <div class="row" *ngFor="let section of getSections(uploader); let i = index">
  57. <div class="col-md-12 col-sm-12 col-xs-12">
  58. <div [formGroupName]="i">
  59. <!---Uploader Section -->
  60. <div class="col-sm-12 col-md-4">
  61. <div class="input-group margin-bottom-sm">
  62. <label>Before:</label>
  63. <input type="file" formControlName="beforeImage" accept=".jpeg, .jpg, .png" class="form-control">
  64. <span style="font-size:12px;">(Formats:jpeg, jpg, png Size: 1MB)</span>
  65. </div>
  66. </div>
  67. <div class="col-sm-12 col-md-4">
  68. <div class="input-group margin-bottom-sm">
  69. <label>Before:</label>
  70. <input type="file" formControlName="afterImage" accept=".jpeg, .jpg, .png" class="form-control">
  71. <span style="font-size:12px;">(Formats:jpeg, jpg, png Size: 1MB)</span>
  72. </div>
  73. </div>
  74. <!---End of Uploader Section -->
  75. </div>
  76. <br>
  77. <button type="button" (click)="addSection()" class="btn btn-info btn-sm">Add More</button>
  78. <button type="button" class="btn btn-danger btn-sm" *ngIf="getSections(uploader).length > 1"
  79. (click)="removeSection(i)">Remove Section
  80. </button>
  81. </div>
  82. </div>
  83. </div>
  84. </div>
  85. <!-- End Section -->
  86. </form>
  87. <pre> {{uploader.value | json}} </pre>
  88. </div>
  89. </div>
  90. </div>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement