From b7d7c00502ae0e9efebfbaee0055041f82fa1ecf Mon Sep 17 00:00:00 2001 From: Yannick Baron Date: Thu, 27 Oct 2016 16:01:56 +0200 Subject: [PATCH] editor.component: using the reactive forms api --- src/app/editor/editor.component.html | 16 ++++----- src/app/editor/editor.component.ts | 51 +++++++++++++++++++++------- 2 files changed, 44 insertions(+), 23 deletions(-) diff --git a/src/app/editor/editor.component.html b/src/app/editor/editor.component.html index bae52df5..f89e8a28 100644 --- a/src/app/editor/editor.component.html +++ b/src/app/editor/editor.component.html @@ -5,30 +5,27 @@ -
+
@@ -37,9 +34,8 @@ + [formControl]="tagField" + (keyup.enter)="addTag()" />
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(event) { - if (event.keyCode === 13) { - this.article.tagList.push(this.tagField); - this.tagField = ''; + addTag() { + // retrieve tag control + let 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) { - this.article.tagList = this.article.tagList.filter((slug) => slug != tagName); + 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( @@ -54,4 +75,8 @@ export class EditorComponent implements OnInit { } ); } + + updateArticle(values: Object) { + (Object).assign(this.article, values); + } }