最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Subscribe to EventEmitter in Angular2 not working - Stack Overflow

programmeradmin2浏览0评论

I'm learning Angular 2. I'm trying to send data from a ponent to other on the click of the first one.

Both ponents are siblings.

This is my code so far:

First Component:

@Component({
  selector: 'jsonTextInput',
  templateUrl: '../templates/jsonTextInput.html',
  directives: [Card, CardTitle, CardDescription, Icon],
  providers: [JsonChangeService]
})

export class JsonTextInput {
  json: string = '';

  constructor (private jsonChangeService: JsonChangeService) {
    this.jsonChangeService = jsonChangeService
  }

  process () {
      this.jsonChangeService.jsonChange(this.json)
  }
}

This is the service:

import {Injectable, EventEmitter} from '@angular/core';
@Injectable()

export default class JsonChangeService {
  public jsonObject: Object;

  stateChange: EventEmitter<any> = new EventEmitter<any>();

  constructor (){
    this.jsonObject = {};
  }

  jsonChange (obj) {
    console.log('sending', obj)
    this.jsonObject = obj
    this.stateChange.emit(this.jsonObject)
  }
}

The call from the first ponent to the service is working, since the sending is being printed.

This is the second Component

@Component({
  selector: 'jsonRendered',
  templateUrl: '../templates/jsonrendered.html',
  directives: [Card, CardTitle],
  providers: [JsonChangeService]
})

export class JsonRendered {
  private jsonObject: Object

  constructor (private jsonChangeService: JsonChangeService) {
    this.jsonChangeService = jsonChangeService
    this.jsonObject = jsonChangeService.jsonObject
    this.jsonChangeService.stateChange.subscribe(json => { this.jsonObject = json; console.log('Change made!') })
  }

  ngOnInit () {
    console.log(1)
  }

  ngOnChanges () {
    console.log(2)
  }

  renderJson () {
    console.log(3)
  }
}

The function inside the subscribe to stateChange never runs. What am I missing?

EDIT

This is the content of my stateChange EventEmitter:

_isAsync: true
_isScalar: false
destination: undefined
dispatching: false
hasCompleted: false
hasErrored: false
isStopped: false
isUnsubscribed: false
observers: Array[0]
source:undefined

I'm learning Angular 2. I'm trying to send data from a ponent to other on the click of the first one.

Both ponents are siblings.

This is my code so far:

First Component:

@Component({
  selector: 'jsonTextInput',
  templateUrl: '../templates/jsonTextInput.html',
  directives: [Card, CardTitle, CardDescription, Icon],
  providers: [JsonChangeService]
})

export class JsonTextInput {
  json: string = '';

  constructor (private jsonChangeService: JsonChangeService) {
    this.jsonChangeService = jsonChangeService
  }

  process () {
      this.jsonChangeService.jsonChange(this.json)
  }
}

This is the service:

import {Injectable, EventEmitter} from '@angular/core';
@Injectable()

export default class JsonChangeService {
  public jsonObject: Object;

  stateChange: EventEmitter<any> = new EventEmitter<any>();

  constructor (){
    this.jsonObject = {};
  }

  jsonChange (obj) {
    console.log('sending', obj)
    this.jsonObject = obj
    this.stateChange.emit(this.jsonObject)
  }
}

The call from the first ponent to the service is working, since the sending is being printed.

This is the second Component

@Component({
  selector: 'jsonRendered',
  templateUrl: '../templates/jsonrendered.html',
  directives: [Card, CardTitle],
  providers: [JsonChangeService]
})

export class JsonRendered {
  private jsonObject: Object

  constructor (private jsonChangeService: JsonChangeService) {
    this.jsonChangeService = jsonChangeService
    this.jsonObject = jsonChangeService.jsonObject
    this.jsonChangeService.stateChange.subscribe(json => { this.jsonObject = json; console.log('Change made!') })
  }

  ngOnInit () {
    console.log(1)
  }

  ngOnChanges () {
    console.log(2)
  }

  renderJson () {
    console.log(3)
  }
}

The function inside the subscribe to stateChange never runs. What am I missing?

EDIT

This is the content of my stateChange EventEmitter:

_isAsync: true
_isScalar: false
destination: undefined
dispatching: false
hasCompleted: false
hasErrored: false
isStopped: false
isUnsubscribed: false
observers: Array[0]
source:undefined
Share Improve this question edited May 28, 2016 at 15:30 Pablo asked May 28, 2016 at 15:17 PabloPablo 10.7k18 gold badges59 silver badges80 bronze badges 10
  • does the call to this.stateChange.next actually emits the event? – FernOfTheAndes Commented May 28, 2016 at 15:26
  • How can I check that? – Pablo Commented May 28, 2016 at 15:27
  • can you not simply call this.stateChange.emit? – FernOfTheAndes Commented May 28, 2016 at 15:28
  • I have changed next to emit It still not working. I have updated my question with the change – Pablo Commented May 28, 2016 at 15:30
  • I also had added the content of the stateChange content – Pablo Commented May 28, 2016 at 15:34
 |  Show 5 more ments

1 Answer 1

Reset to default 5

You have two different instances of JsonChangeService. That's why you don't receive message between ponents. You need to have one instance service, i.e. on parent ponent or on top level like this:

bootstrap(AppComponent, [JsonChangeService])
发布评论

评论列表(0)

  1. 暂无评论