发表于: 2020-01-05 22:11:58

1 1327


今日完成:




import { ComponentOnInit } from '@angular/core';
import {HeroServicefrom '../hero.service';
import {Herofrom '../hero';
@Component({
  selector: 'app-heroes',
  templateUrl: './heroes.component.html',
  styleUrls: ['./heroes.component.css']
})
export class HeroesComponent implements OnInit {

  heroesHero[];
  constructor(private heroServiceHeroService) { }


  add(name:string):void {
    name=name.trim();
    if(!name) {
      return;
    }
    this.heroService.addHero({nameas Hero)
    .subscribe(hero=> {
      this.heroes.push(hero);
    });
  }
  
  getHeroes(): void {
    this.heroService.getHeroes()
    .subscribe(heroes => this.heroes = heroes);
    console.log('heroes');
  }

 delete(hero:Hero):void {
   this.heroesthis.heroes.filter(h=>h !==hero);
   this.heroService.deleteHero(hero).subscribe();
 }
 

  ngOnInit() {
    this.getHeroes();
  }

}




/* HeroesComponent's private CSS styles */
.heroes {
  margin0 0 2em 0;
  list-style-typenone;
  padding0;
  width15em;
}
.heroes li {
  positionrelative;
  cursorpointer;
  background-color#EEE;
  margin.5em;
  padding.3em 0;
  height1.6em;
  border-radius4px;
}

.heroes li:hover {
  color#607D8B;
  background-color#DDD;
  left.1em;
}

.heroes a {
  color#333;
  text-decorationnone;
  positionrelative;
  displayblock;
  width250px;
}

.heroes a:hover {
  color:#607D8B;
}

.heroes .badge {
  displayinline-block;
  font-sizesmall;
  colorwhite;
  padding0.8em 0.7em 0 0.7em;
  background-color:#405061;
  line-height1em;
  positionrelative;
  left-1px;
  top-4px;
  height1.8em;
  min-width16px;
  text-alignright;
  margin-right.8em;
  border-radius4px 0 0 4px;
}

button {
  background-color#eee;
  bordernone;
  padding5px 10px;
  border-radius4px;
  cursorpointer;
  cursorhand;
  font-familyArial;
}

button:hover {
  background-color#cfd8dc;
}

button.delete {
  positionrelative;
  left194px;
  top-32px;
  background-colorgray !important;
  colorwhite;
}



import { ComponentOnInit,Input } from '@angular/core';
import {Herofrom '../hero';
import {ActivatedRoutefrom '@angular/router';
import {Locationfrom '@angular/common';
import {HeroServicefrom '../hero.service';
import { Observable } from 'rxjs';
@Component({
  selector: 'app-hero-detail',
  templateUrl: './hero-detail.component.html',
  styleUrls: ['./hero-detail.component.css']
})


export class HeroDetailComponent implements OnInit {

   heroHero;

  constructor(
    private routeActivatedRoute,
    private heroServiceHeroService,
    private locationLocation,
  ) { }

  ngOnInit() {
    this.getHero();
    console.log('hero-detail');
  }

  getHero():void {
    const id = +this.route.snapshot.paramMap.get('id');
    this.heroService.getHero(id)
    .subscribe(hero => this.hero = hero);
    console.log(id);
    console.log('getHero()');
  }

  goBack():void {
    this.location.back();
  }

 
  save(): void {
    this.heroService.updateHero(this.hero)
    .subscribe(()=>this.goBack);
  }

}



返回列表 返回列表
评论

    分享到