'typeof Foods'类型不存在属性'id'

时间:2017-01-05 10:11:39

标签: php angularjs angular

查看下面的代码,我试图使用复选框从我的数据表中删除客户端。当我勾选一个复选框时,我在控制台中看到对象的属性已从数据库中获取。我试图通过id删除但终端引发此错误“属性'id''在类型'typeof Clients'上不存在”。我搜索后还没有找到任何解决方案。有什么帮助吗?

// Http服务

@Injectable()

 export class HttpService {

  constructor(private http:Http) {

  }



  deleteFood(food) {
    return this.http.delete('http://localhost:9000/api/v1/food/${id}')
      .map((response:Response) => response.json())
  }
}

//表

 <tbody>
    <tr *ngFor="let food of Foods; let i = index">
      <td><input #{{food.id}} [(ngModel)]="food.selected" type="checkbox" (change)="checkbox(food)"></td>

      <td>{{client.type}}</td>
      <td>{{client.location}}</td>

    </tr>

  </tbody>
</table>

<button type="button" (click)="deleteFood()">Delete</button>

//组件

  export class FoodComponent {

  Foods : Foods[] = []; 



  constructor(private httpService: HttpService, private router: Router){



   selectedFood = Foods;

   deleteFood(){
    this.httpService.deleteFood(this.selectedFood.id)
     .subscribe(data =>{
       console.log(data)
     })
  }

  checkbox(food){
    food.selected = (food.selected) ? false: true;
    this.selectedFood = food;
    console.log(this.selectedFood);
  }
}

// Food.ts

  export class Food{

  constructor(
    public type:string,
    public location: string,

 ){}
}

1 个答案:

答案 0 :(得分:0)

在您的组件中,您将Clients类型分配给activeClient属性,而不是定义其类型。并且您的Clients类没有静态属性id,这就是编译器在您访问this.activeClient.id时抱怨的原因。

如果你改变:

activeClient = Clients;

为:

activeClient: Clients;

你不应该再遇到问题了。

相关问题