This repository was archived by the owner on Aug 20, 2024. It is now read-only.
forked from realworld-apps/angular-realworld-example-app
-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy patheditor.component.ts
More file actions
82 lines (70 loc) · 2.09 KB
/
Copy patheditor.component.ts
File metadata and controls
82 lines (70 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, FormControl } from '@angular/forms';
import { ActivatedRoute, Router } from '@angular/router';
import { Article, ArticlesService } from '../core';
@Component({
selector: 'app-editor-page',
templateUrl: './editor.component.html'
})
export class EditorComponent implements OnInit {
article: Article = {} as Article;
articleForm: FormGroup;
tagField = new FormControl();
errors: Object = {};
isSubmitting = false;
constructor(
private articlesService: ArticlesService,
private route: ActivatedRoute,
private router: Router,
private fb: FormBuilder
) {
// use the FormBuilder to create a form group
this.articleForm = this.fb.group({
title: '',
description: '',
body: ''
});
// Initialized tagList as empty array
this.article.tagList = [];
// Optional: subscribe to value changes on the form
// this.articleForm.valueChanges.subscribe(value => this.updateArticle(value));
}
ngOnInit() {
// If there's an article prefetched, load it
this.route.data.subscribe((data: { article: Article }) => {
if (data.article) {
this.article = data.article;
this.articleForm.patchValue(data.article);
}
});
}
addTag() {
// retrieve tag control
const tag = this.tagField.value;
// only add tag if it does not exist yet
if (this.article.tagList.indexOf(tag) < 0) {
this.article.tagList.push(tag);
}
// clear the input
this.tagField.reset('');
}
removeTag(tagName: string) {
this.article.tagList = this.article.tagList.filter(tag => tag !== tagName);
}
submitForm() {
this.isSubmitting = true;
// update the model
this.updateArticle(this.articleForm.value);
// post the changes
this.articlesService.save(this.article).subscribe(
article => this.router.navigateByUrl('/article/' + article.slug),
err => {
this.errors = err;
this.isSubmitting = false;
}
);
}
updateArticle(values: Object) {
Object.assign(this.article, values);
}
}