복붙노트

[ANGULAR] Angular2의 행동, 작동 방식 및 사용 방법

ANGULAR

Angular2의 행동, 작동 방식 및 사용 방법

해결법


  1. 1.하나의 속성으로 축소되어야합니다. 나는 이벤트 값에 대해 xxxservice 형식을 사용하도록 나에게 의미가 없기 때문에 SharedService를 문자열로 변경했습니다.

    하나의 속성으로 축소되어야합니다. 나는 이벤트 값에 대해 xxxservice 형식을 사용하도록 나에게 의미가 없기 때문에 SharedService를 문자열로 변경했습니다.

    import {Injectable}     from 'angular2/core';
    import {BehaviorSubject} from 'rxjs/BehaviorSubject';
    
    @Injectable()
    export class SearchService {
    
        public space: Subject<string> = new BehaviorSubject<string>(null);
    
        broadcastTextChange(text:string) {
            this.space.next(text);
        }
    }
    
    @Component({
      selector: 'some-component'
      providers: [SearchService], // only add it to one common parent if you want a shared instance
      template: `some-component`)}
    export class SomeComponent {
      constructor(searchService: SearchService) {
        searchService.space.subscribe((val) => {
          console.log(val); 
        });
      }
    }
    
  2. from https://stackoverflow.com/questions/36404541/behavioursubject-in-angular2-how-it-works-and-how-to-use-it by cc-by-sa and MIT license