Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

322
Visualizações
How to add a part of the acquired API to the array defined in component

For learning, I am trying to create a simple Todo application using Angular 2 + Django Rest Framework.
In order to immediately display the items saved in the input form, I want to implement the function of acquiring the latest one and displaying it.

JSON format WEB API for acquiring the latest one has the following structure. enter image description here

function for getting this API is described in todo.service.ts

@Injectable()
export class TodoService {
  todo: Todo[] = [];
  newtodo: NewTodo[] = [];
  private Url = `http://127.0.0.1:8000/api/todo/`
  private headers = new Headers({'Content-Type': 'application/json'});

  constructor(
    private http: Http
  ){}

  // Acquire one latest todo added
  getNewTodo(): Promise<NewTodo[]> {
    return this.http
      .get(this.Url+"?limit=1")
      .toPromise()
      .then(res => res.json())
      .catch(this.handleError)
  }

Components are described in todo.component.ts

@Component({
  selector: 'todo-list',
  templateUrl: '../templates/todo-list.component.html',
  styleUrls: ['../static/todo-list.component.css']
})
export class TodoListComponent {
  todos: Todo[] = [];
  newtodo: NewTodo[] = [];
  newtodos: Todo[] = [];
  @Input() todo: Todo = new Todo();

  save(): void {
    this.todoService
      .create(this.todo);
  }
}

The point I want to teach is the following points.
1.Execute service's getNewTodo () when save () of the component is executed and store the obtained return value in the array newtodo.
2.Since only the results part is necessary for the acquired JSON, pull out the results part, push it to the array newtodos and pass it to html.

For 1, I think that you should write the following description in save ()

this.todoService
  .getNewTodo()
  .then(newtodo => this.newtodo = newtodo);

I can not understand what to write about 2. I'd like to tell you how to solve it.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You can go into the JSON and get the content of the results with the following:

 getNewTodo(): Promise<NewTodo[]> { return this.http .get(this.Url+"?limit=1") .toPromise() .then(res => res.json().results) // here .catch(this.handleError) }

You're not showing the use of the create method on the service, but whatever it is, return a promise, so that we in the component can within the callback call getNewTodo :

 save() { this.todoService .create(this.todo) .then(data => { this.getNewTodo(); // here }) }

and the getNewTodo which calls the method on the service:

 getNewTodo() { this.todoService .getNewTodo() .then(newtodo => { this.newtodo = newtodo }); }
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda