如何在angular2中完成http后触发eventemitter

时间:2016-01-26 11:42:12

标签: angular

我将这两个设置在一个组件中。

string script = @"param([string]$inParam)
$Username = 'SERVER\domain_user'
$Password = 'password'
$pass = ConvertTo-SecureString -AsPlainText $Password -Force
$Cred = New-Object System.Management.Automation.PSCredential -ArgumentList $Username,$pass
Invoke-Command -ComputerName SERVER_TEST -Authentication Default -ScriptBlock {&""D:\Programs\myapplication.cmd"" $inParam} -Credential $Cred";

当我运行下面的http.get时,如何触发卡$ .emit(this.cards);

  cards$: EventEmitter<any> = new EventEmitter();
  private cards: Array<Cards>;

2 个答案:

答案 0 :(得分:4)

就像你说cards$.emit(this.cards);一样。将此添加到您的订阅onNext()回调:

http.get('http://myendpoint')
  .map((res: Response) => res.json())
  .subscribe(res => {
    this.cards = res;
    this.cards$.emit(this.cards);
  };

您还应该在其他地方订阅它以使用此值,例如:

constructor() {
  this.cards$.subscribe(c => console.log(c));
}

答案 1 :(得分:0)

添加完整的回调:

  this.cards = [];

  http.get('http://myendpoint')
    .map((res: Response) => res.json())
    .subscribe(
       res => { this.cards = res }, 
       error => { console.log(error) },
       () => this.cards$.emit(this.cards));

如果您期望多个事件并希望在最后一个事件之后执行代码,则此方法有效。从Http你只能得到一个事件,在这种情况下@Sasxa的方法可能更容易。