呼叫服务改变路线的角度

时间:2018-05-09 12:23:23

标签: javascript angular

我是angular2中的新手并且创建了aproject usibg angularjs.在我的应用程序中,我想在用户使用params更改路径时调用该服务。当我第一次来时,服务电话成功,但当我改变paramteres而不是我的服务没有打电话。这是我的代码:

constructor(private route:ActivatedRoute,
private service: CourseService

){}
    private player: Player;
    id:'';
    coursesData:'';
    courseDetail:'';
  getModulesData:'';


    ngOnInit() {
     this.route.params.subscribe(params =>
       this.id = params["id"]
    )
    var courseList = JSON.parse(localStorage.getItem("courses"));
    this.coursesData = courseList.data;
    for(var i=0; i<courseList.data.length; i++){
        if(courseList.data[i].course_id == 4){
            this.courseDetail = courseList.data[i]
            break;
        }
    }
    this.service.getCourseDetails(this.id).subscribe((courseDetails:any) =>{

        this.getModulesData = courseDetails.data
    })
    setTimeout(function(){
   this.player = new Player("player");
     }, 0);
  }


}

1 个答案:

答案 0 :(得分:0)

ngOnInit() {
  this.route.params.subscribe(params =>
    this.id = params["id"]

    var courseList = JSON.parse(localStorage.getItem("courses"));
    this.coursesData = courseList.data;
    for(var i=0; i<courseList.data.length; i++){
        if(courseList.data[i].course_id == 4){
            this.courseDetail = courseList.data[i]
            break;
        }
    }
  this.service.getCourseDetails(this.id).subscribe((courseDetails:any) =>{
    this.getModulesData = courseDetails.data
   })
    setTimeout(function(){
       this.player = new Player("player");
    }, 0);
  )
}