Javascript Angular5-从类中新创建的对象返回未定义

Javascript Angular5-从类中新创建的对象返回未定义,javascript,typescript,angular5,Javascript,Typescript,Angular5,我有一个服务类,其中我从组件传递一个对象,并将其临时保存到模型类中。我想从讲师.ts(模型)处获得讲师.service.ts中的$key 讲师.组件.ts onItemClick(lec: Lecturer) { this.lecturerService.selectedLecturer = Object.assign({},lec); // this is working properly as i have seen in console.log(lec) } export class

我有一个服务类,其中我从组件传递一个对象,并将其临时保存到模型类中。我想从讲师.ts(模型)处获得讲师.service.ts中的$key

讲师.组件.ts

onItemClick(lec: Lecturer) {
  this.lecturerService.selectedLecturer = Object.assign({},lec); // this is working properly as i have seen in console.log(lec)
}
export class Lecturer {
    $key: string;
    userName: string;
    userEmail: string;
    userProfileImageUrl: string;
}
 import { Injectable } from '@angular/core';
 import { Lecturer } from './lecturer';
 import { AngularFireDatabase, AngularFireList } from '@angular/fire/database';

 @Injectable({
    providedIn: 'root'
 })

  export class LecturerService {
     lecturerList: AngularFireList<any>;
     assignedCourseList: AngularFireList<any>;

     selectedLecturer: Lecturer = new Lecturer();

     constructor(private db: AngularFireDatabase) {}

     getAssignedCourse() {
     this.assignedCourseList = this.db.list('Users/' + this.selectedLecturer.$key + '/Course'); //this is where i'm having problem to get the $key
       console.log(this.selectedLecturer.$key);
       return this.assignedCourseList;
     }
  }
讲师。ts

onItemClick(lec: Lecturer) {
  this.lecturerService.selectedLecturer = Object.assign({},lec); // this is working properly as i have seen in console.log(lec)
}
export class Lecturer {
    $key: string;
    userName: string;
    userEmail: string;
    userProfileImageUrl: string;
}
 import { Injectable } from '@angular/core';
 import { Lecturer } from './lecturer';
 import { AngularFireDatabase, AngularFireList } from '@angular/fire/database';

 @Injectable({
    providedIn: 'root'
 })

  export class LecturerService {
     lecturerList: AngularFireList<any>;
     assignedCourseList: AngularFireList<any>;

     selectedLecturer: Lecturer = new Lecturer();

     constructor(private db: AngularFireDatabase) {}

     getAssignedCourse() {
     this.assignedCourseList = this.db.list('Users/' + this.selectedLecturer.$key + '/Course'); //this is where i'm having problem to get the $key
       console.log(this.selectedLecturer.$key);
       return this.assignedCourseList;
     }
  }
现在我只想从服务类的teachle.ts中得到$key,但不知怎的,我无法得到这个函数中使用的key

讲座.服务.ts

onItemClick(lec: Lecturer) {
  this.lecturerService.selectedLecturer = Object.assign({},lec); // this is working properly as i have seen in console.log(lec)
}
export class Lecturer {
    $key: string;
    userName: string;
    userEmail: string;
    userProfileImageUrl: string;
}
 import { Injectable } from '@angular/core';
 import { Lecturer } from './lecturer';
 import { AngularFireDatabase, AngularFireList } from '@angular/fire/database';

 @Injectable({
    providedIn: 'root'
 })

  export class LecturerService {
     lecturerList: AngularFireList<any>;
     assignedCourseList: AngularFireList<any>;

     selectedLecturer: Lecturer = new Lecturer();

     constructor(private db: AngularFireDatabase) {}

     getAssignedCourse() {
     this.assignedCourseList = this.db.list('Users/' + this.selectedLecturer.$key + '/Course'); //this is where i'm having problem to get the $key
       console.log(this.selectedLecturer.$key);
       return this.assignedCourseList;
     }
  }
从'@angular/core'导入{Injectable};
从“/讲师”导入{讲师};
从'@angular/fire/database'导入{AngularFireDatabase,AngularFireList};
@注射的({
providedIn:'根'
})
出口类讲师服务{
讲师名单:安格尔消防队员;
指定的课程教师:AngularFireList;
Selected讲师:讲师=新讲师();
构造函数(私有数据库:AngularFireDatabase){}
getAssignedCourse(){
this.assignedCourseList=this.db.list('Users/'+this.selected讲师.$key+'/Course');//这就是我在获取$key时遇到问题的地方
console.log(此.selected讲师.$key);
返回此.assignedCourseList;
}
}

我能做些什么来实现这一点

解决方案1:

我想你应该像这样把默认值传递给它

讲师.ts文件

export class Lecturer {
   $key: string = '';
   userName: string = '';
   userEmail: string = '';
   userProfileImageUrl: string = '';
}
新对象创建

selectedLecturer: Lecturer = new Lecturer();

console.log(selectedLecturer)
// output will be
 { $key: '',
   userName: '',
   userEmail:'',
   userProfileImageUrl:''
 }
selectedLecturer: Lecturer = new Lecturer('my_key','name','email','url');

console.log(selectedLecturer)
// output will be
 { $key: 'my_key',
   userName: 'name',
   userEmail:'email',
   userProfileImageUrl:'url'
 }
解决方案2:

touch.ts文件

export class Lecturer {
   $key: string = '';
   userName: string = '';
   userEmail: string = '';
   userProfileImageUrl: string = '';
}
或者在创建一个新对象时添加一个构造函数来赋值

新对象创建

selectedLecturer: Lecturer = new Lecturer();

console.log(selectedLecturer)
// output will be
 { $key: '',
   userName: '',
   userEmail:'',
   userProfileImageUrl:''
 }
selectedLecturer: Lecturer = new Lecturer('my_key','name','email','url');

console.log(selectedLecturer)
// output will be
 { $key: 'my_key',
   userName: 'name',
   userEmail:'email',
   userProfileImageUrl:'url'
 }

我猜您在这个中有未定义的值。selected讲师。$key。控制台this.selected讲师。是,我有未识别的值。但是获得价值的方式是什么呢。我也试过这样,这个。selected讲师['$key']也不起作用。你只是把讲师的一个空对象设为selected讲师。您必须将一个对象分配给this.讲师服务.selected讲师=对象.assign({},lec);我想我是在我的讲师.component.ts中这样做的,控制台显示了所有的值。但是我在演讲中听不到。服务。t真的谢谢你的建议。但它不起作用(好的,肯定还有另一个问题。但是这是基于类的对象创建的标准方法。顺便问一下,您从哪里调用getAssignedCourse()函数??我明白了。事实上,我可以用它从其他模块以表单的形式从Selected讲师处获取数据,虽然我使用了双向绑定,但我不知怎么地认为这会起作用。但这不起作用。我是从assignedcourse.module.ts调用它的。