• [Angular] Saving draft form into Cookies


    npm install cookies-js --save
    import {Component, OnInit} from '@angular/core';
    import {FormGroup, FormBuilder, Validators} from "@angular/forms";
    import * as Cookies from 'cookies-js';
    
    @Component({
        selector: 'create-lesson',
        templateUrl: './create-lesson.component.html',
        styleUrls: ['./create-lesson.component.css']
    })
    export class CreateLessonComponent implements OnInit {
    
        private static readonly DRAFT_COOKIE = 'create-lesson-draft';
    
        form: FormGroup;
    
        constructor(private fb: FormBuilder) {
    
            this.form = this.fb.group({
                description: ['',Validators.required],
                url: ['',Validators.required],
                longDescription: ['']
            });
    
        }
    
        ngOnInit() {
    
            const draft = Cookies.get(CreateLessonComponent.DRAFT_COOKIE);
    
            if (draft) {
                this.form.setValue(JSON.parse(draft));
            }
    
            this.form.valueChanges
                .filter(() => this.form.valid)
                .do(validValue => Cookies.set(
                    CreateLessonComponent.DRAFT_COOKIE,
                    JSON.stringify(validValue)) )
                .subscribe();
    
        }
    
    }
  • 相关阅读:
    2013年 积木大赛
    Matlab 画图2
    Matlab 画图1
    Matlab 数值计算
    Matlab 条件循环函数
    Non Super Boring Substring 题解(hash+思维)
    Matlab 数组
    Matlab 矩阵
    L
    hdu 1671Phone List
  • 原文地址:https://www.cnblogs.com/Answer1215/p/13843897.html
Copyright © 2020-2023  润新知