2016-06-06 6 views
1

내가가는대로 HTTP를 배우고 있습니다. 앞으로 나아갈 방법을 모릅니다. 나는 내 서비스에서만 http 전화를하고있다.HTTP : 업데이트/저장/삭제시 하위 구성 요소의 변경 사항을 감지합니다.

export class Teacher { 

    public addStudent(value: Student): void { 
     this.students.push(value); 
    } 
} 

내가 선생님을 나열하는 목록 구성 요소를 가지고 있고, 그 구성 요소, 사용자가 교사를 클릭하고 세부로 이동 : 다음

여기 몇 가지 방법입니다, 클래스, 교사가 페이지에서 사용자 입력을 받아 학생을 교사에게 추가합니다.

export class TeacherDetailComponent implements OnActivate { 

    teacher: Teacher; 

    constructor(public _service: Service, public _router: Router) { } 

    routerOnActivate(curr: RouteSegment): void { 
     let id = curr.getParam('id'); 
     this._service.getById(id) 
      .subscribe(teacher => { 
       this.teacher = teacher; 
      }); 
    } 

    addStudent() { 
     this.teacher.getStudents().push(new Student()); 
     //what code here? 
    } 
} 

내 headscratcher가있다, 어떻게 내가 새로운 학생이 추가 될 때 교사에 대한 데이터를 업데이트 할 수 있다는 각도 이야기한다!

+0

당신이 당신의'TeacherDetailComponent' 구성 요소의 템플릿을 추가 할 수 있습니다 목록 구성 요소에서

addStudent() { let newStudent = new Student(); this.studentService.addStudent(newStudent).subscribe(addedStudent => { this.teacher.getStudents().push(addedStudent); }); } 

:

@Injectable() export class StudentService { userAdded:Subject<Student> = new Subject(); userDeleted:Subject<Student> = new Subject(); constructor(private http:Http) { } addStudent(student:Student) { return this.http.post('/users', ...) (...) .do((addedStudent) => { this.userAdded.next(addedStudent); }); } deleteStudent(student:Student) { return this.http.post('/users', ...) (...) .do((removedStudent) => { this.userRemoved.next(removedStudent); }); } } 

그래서 당신은 당신의 세부 구성 요소를 업데이트 할 수 있습니까? –

+0

"사용 완료"란 정확히 무엇을 의미합니까? –

+0

@ GünterZöchbauer 그래, 그게 사실을 지적하지는 않았지만, 사실 내 질문의 일부입니다. 나는 그것이 사용자가하는 모든 변화에서 교사를 업데이트해야한다고 생각한다. – Alex

답변

2

사실 귀하의 질문은 구성 요소 통신과 관련됩니다. 나는 이것을위한 공유 된 서비스를 만들 것이다.

자세한 내용은이 문서를 참조하십시오 :

그래서 나는 학생이 추가되거나 제거되는 목록 구성 요소를 통지하는 서비스를 만들 것, 그에 따라 목록을 업데이트 할 수 있도록 . 다음은 샘플입니다

this.studentService.addedStudent.subscribe(addedStudent => { 
    // do something 
}); 
+0

나는 아직도 이것을 이해하려고 노력하고 있기 때문에 나의 질문을 업데이트했다 ... 만약 당신이 그것을 보았다면, 나는 정말로 감사 할 것이다! – Alex

+0

학생이 특정 교사를 위해 업데이트 될 때 교사 목록 페이지에서 업데이트 할 내용이 있습니까? –

+0

그게 내가 왜 코드를 추가해야하는지 궁금해하는 이유입니다. 이 코드는 'isSkipSelf null'속성을 읽을 수 없습니다.라는 오류가 발생합니다. 나는 오류 코드를 보면서 호 전적이다. ... – Alex

관련 문제