import { Component , ElementRef , OnInit , ViewChild } from '@angular/core' ; import { NgForm } from '@angular/forms' ; import { ActivatedRoute , Router } from '@angular/router' ; import { NzNotificationService } from 'ng-zorro-antd' ; import { ArticleModel , ITag } from '../model/article.model' ; import { EditArticleService } from './editarticle.service' ; @ Component ( { selector : 'app-edit-aritcle' , templateUrl : './editarticle.html' , } ) export class EditArticleComponent implements OnInit { public isConfirmLoading = false ; public mainModel : ArticleModel = new ArticleModel ( ) ; @ ViewChild ( 'form' , { static : false } ) private form : NgForm ; public tagList : ITag [ ] = [ ] ; public _tagList : object [ ] = [ ] ; public checkedTag = { } ; public typeAjaxList : any [ ] = [ ] ; constructor ( private router , private route , private editArticleService , private notification ) { } public ngOnInit ( ) { this . route . params . subscribe ( ( param ) => { if ( param . id ) { this . getData ( param . id ) ; } this . getTypeList ( ) ; this . getTagList ( ) ; } , ( err ) => { this . notification . warning ( '错误' , '参数错误' ) ; } ) ; } public getData ( id ) { this . editArticleService . getArticle ( id ) . subscribe ( ( res ) => { this . mainModel = res . data ; res . data . tag . split ( ',' ) . forEach ( ( v ) => { this . checkedTag [ v ] = true ; } ) ; } , ( err ) => { } ) ; } public getPics ( url ) { return ! ! url ? url + '?Authorization-User=' + localStorage . getItem ( 'ACCESS_TOKEN' ) : '' ; } public save ( ) { for ( const i in this . form . controls ) { this . form . controls [ i ] . markAsDirty ( ) ; } this . mainModel . tag = this . getCheckedTag ( ) ; if ( this . form . valid ) { this . isConfirmLoading = true ; this . editArticleService . updateArticle ( this . mainModel ) . finally ( ( ) => this . isConfirmLoading = false ) . subscribe ( ( res ) => { this . router . navigate ( [ '/article' ] ) ; } , ( err ) => { } ) ; } } public getTypeList ( ) { this . editArticleService . getTypes ( ) . subscribe ( ( res ) => { this . typeAjaxList = res . data ; } , ( err ) => { } ) ; } public getTagList ( ) { this . editArticleService . getTags ( ) . subscribe ( ( res ) => { this . tagList = res . data ; } , ( err ) => { } ) ; } public getCheckedTag ( ) { return this . tagList . filter ( ( t ) => { return this . checkedTag [ t . name ] ; } ) . map ( ( t ) => { return t . name ; } ) . join ( ',' ) ; } public back ( ) { this . router . navigate ( [ 'article' ] ) ; } }