douqun1977 2019-02-21 10:57
浏览 31

在构造函数中以角度7设置组件变量,并在方法中正确检索它而不重复

The problem: I need to get some member variable, but to be more clear its value. As a result I get the wanted variable value, but when I console.log this variable it gives some strange output which I think is nor correct to recieve.

to-do.service.ts

import { Injectable } from '@angular/core';
import { ToDo } from '../interfaces/to-do';
import {HttpClient} from '@angular/common/http';
import {filter} from 'rxjs/operators';

const API_URL = 'http://localhost:8000/api/';

@Injectable({
  providedIn: 'root'
})
export class ToDoService {
  todoTitle: string = '';
  idForTodo: number = 4;
  beforeEditCache: string = '';
  filter: string = 'all';
  anyRemainingModel: boolean = true;
  todos: ToDo[] = [];

  constructor(private http: HttpClient) {
   this.todos = this.getToDos();
   console.log(this.todos);
}

getToDos(): ToDo[] {
  this.http.get(API_URL + 'getData.php')
  .subscribe((response: any ) => {
      this.todos = response;
  });
return this.todos;
}

addToDo(todoTitle: string): void {
  if (todoTitle.trim().length === 0) {
    return;
  }

  this.todos.push({
    id: this.idForTodo,
    title: todoTitle,
    completed: false,
    editing: false
  })

  this.idForTodo++;
}

editTodo(todo: ToDo): void {
   this.beforeEditCache = todo.title;
   todo.editing = true;
}

doneEdit(todo: ToDo): void {
  if (todo.title.trim().length === 0) {
    todo.title = this.beforeEditCache;
  }

  this.anyRemainingModel = this.anyRemaining();
  todo.editing = false;
}

cancelEdit(todo: ToDo): void {
  todo.title = this.beforeEditCache;
  todo.editing = false;
}

deleteTodo(id: number): void {
  this.todos = this.todos.filter(todo => todo.id !== id);
}

remaining(): number {
  return this.todos.filter(todo => !todo.completed).length;
}

atLeastOneCompleted(): boolean {
  console.log(this.todos);
  return true;
}

clearCompleted(): void {
  this.todos = this.todos.filter(todo => !todo.completed);
}

checkAllTodos(): void {
   this.todos.forEach(todo => todo.completed = (<HTMLInputElement> event.target).checked)
   this.anyRemainingModel = this.anyRemaining();
}

anyRemaining(): boolean {
  return this.remaining() !== 0;
}

todosFiltered(): ToDo[] {
   if (this.filter === 'all') {
    return this.todos;
   } else if (this.filter === 'active') {
    return this.todos.filter(todo => !todo.completed);
   } else if (this.filter === 'completed') {
    return this.todos.filter(todo => todo.completed);
   }

  return this.todos;
 }
}    

to-do-list.component.html

<input type="text" class="todo-input" placeholder="What needs to be done" [(ngModel)]="todoTitle" (keyup.enter)="addTodo()">

<app-to-do-item
   *ngFor="let todo of todoService.todosFiltered()"
   [todo]="todo"
 ></app-to-do-item>

<div class="extra-container">
  <div><label><input type="checkbox" 
  (change)="todoService.checkAllTodos()"  
  [(ngModel)]="!todoService.anyRemainingModel"> Check All</label> 
</div>
<div>{{ todoService.remaining() }} items left</div>
</div>

<div class="extra-container">
   <div>
     <button [ngClass]="{'active': todoService.filter === 'all'}" 
        (click)="todoService.filter='all'">All</button>
      <button [ngClass]="{'active': todoService.filter === 'active'}" 
        (click)="todoService.filter='active'">Active</button>
      <button [ngClass]="{'active': todoService.filter === 
        'completed'}" 
        (click)="todoService.filter='completed'">Completed</button>
    </div>

   <div *ngIf="todoService.atLeastOneCompleted()">
        <button (click)="todoService.clearCompleted()">Clear 
         Completed</button>
   </div>

 </div>

getData.php

<?php

require 'DatabaseHelper.php';

$dbHelper = new DatabaseHelper();
$queryWorker = $dbHelper->connect();

$sql = "SELECT * FROM todos";

$result = $queryWorker->query($sql);

$result = $result->fetchAll();

header("Access-Control-Allow-Origin: *");
header('Content-Type: application/json');
echo json_encode($result);

And here is it why I am receiving this strange output? Please someone help me to understand how to properly set this.todos member variable so when I call it in other methods I will receive proper data with proper quantity.

image for strange output: https://i.stack.imgur.com/owXHj.png

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 基于卷积神经网络的声纹识别
    • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
    • ¥100 为什么这个恒流源电路不能恒流?
    • ¥15 有偿求跨组件数据流路径图
    • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
    • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
    • ¥15 CSAPPattacklab
    • ¥15 一直显示正在等待HID—ISP
    • ¥15 Python turtle 画图
    • ¥15 stm32开发clion时遇到的编译问题