angular2 - aspnet core:对象属性undefined

时间:2017-03-28 17:34:05

标签: asp.net-mvc angular

我在客户端使用角度2,在服务器端使用aspnet核心。我试图从数据库中删除某个项目,但不知怎的,我无法这样做,因为:

QueryDslPredicateExecutor

我在这里做错了什么?

Myproject.services.ts

itemToDelete.projectId is undefined.

myProjects.component.ts

@Injectable()
export class MyProjectsService {
private url: string = 'api/myprojects';

constructor(private http: Http, private authService: AuthService) { }

///->itemToDelete.projectID undefined///

deleteProject(itemToDelete: Project) {
    return this.http.delete(this.url + '/' + itemToDelete.projectId, { headers: this.authService.authJsonHeaders() })
               .map((res: Response) => res.json())
               .catch(this.handleError);
}

project.ts

export class MyProjectsComponent implements OnInit {
    myProjectsList: Project[] = [];
    selectedItem: Project = null;
    project: Project = null;

constructor(private myProjectsService: MyProjectsService, private toastrService: ToastrService) { }

initProject(): Project {
    var newProject = new Project();
    return newProject;
}

ngOnInit() {
    this.getMyProjects();
    this.project = this.initProject();
}

selectCurrentItem(thisItem: Project, event: any) {
    event.preventDefault();
    this.selectedItem = thisItem;
    this.project = Object.assign({}, thisItem);
}

deleteProject(itemToDelete: Project, event: any) {
    event.preventDefault();
    this.myProjectsService.deleteProject(itemToDelete)
        .subscribe((data: ViewModelResponse) => {
            if (data != null && data.statusCode == 200) {
                this.showSuccess('Delete', data.value);
                this.getMyProjects();
            }
            else {
                this.showError('Delete', "An error occurred");
            }
        },
        (error: any) => {
            this.showError('Delete', JSON.stringify(error));
        });
}

myProjectsComponent.chtml

export class Project {

projectId : number;
userId: number;
projectName: string;
}

1 个答案:

答案 0 :(得分:0)

尝试定义如下,

当你说null时,它被指定为null,

export class MyProjectsComponent implements OnInit {
    myProjectsList: Project[] = [];
    selectedItem: Project;
    project: Project;
}