Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { Component, OnInit, Output, EventEmitter, Input } from '@angular/core';
- import { FormGroup, FormControl, FormArray } from '@angular/forms';
- import { Comment } from '../models/comment.model';
- import { CommentService } from '../services/comment.service';
- @Component({
- selector: 'app-comment-form',
- templateUrl: './comment-form.component.html',
- styleUrls: ['./comment-form.component.css']
- })
- export class CommentFormComponent implements OnInit {
- @Input('commentIndex') commentIndex: number;
- private comment: Comment;
- private form: FormGroup;
- constructor(private commentService: CommentService) {
- this.form = new FormGroup({
- content: new FormControl(),
- attachments: new FormControl([]),
- });
- }
- ngOnInit() {
- if (this.commentIndex) {
- console.log(this.commentIndex);
- //this.initComment(this.commentIndex);
- } else {
- this.initEmptyComment();
- }
- }
- private addComment() {
- for (let property in this.form.controls) {
- this.comment[property] = this.form.get(property).value;
- }
- this.commentService.addComment(this.comment);
- }
- private initEmptyComment() {
- this.comment = new Comment(null, null, null, null, null, null);
- }
- private initComment(index: number) {
- this.commentService.getSingleComment(index).subscribe(comment => {
- this.comment = comment;
- this.form.get('content').patchValue(this.comment.content);
- this.form.get('attachments').patchValue(this.comment.attachments);
- });
- }
- private onFileChange(event) {
- let reader = new FileReader();
- let attachments = this.form.get('attachments');
- if (event.target.files && event.target.files.length > 0) {
- for (let file of event.target.files) {
- console.log(attachments.value);
- reader.readAsDataURL(file);
- reader.onload = () => {
- attachments.value.push({
- fileName: file.name,
- fileType: file.type,
- value: reader.result.split(',')[1]
- });
- }
- }
- }
- }
- private removeFile(removingFile: any) {
- if (removingFile) {
- let attachments = this.form.get('attachments').value;
- for (let file in attachments) {
- if (attachments[file].fileName == removingFile.fileName) {
- (attachments as Array<any>).splice(attachments.indexOf(attachments[file]), 1);
- }
- }
- }
- }
- get attachments() {
- return this.form.get('attachments');
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement