我有2个兄弟组件,我在一个组件中做了一个http请求,如果发生特定情况,它应该发出另一个http请求,写入另一个组件。所以我应该可以在第一个组件中调用该方法。角度4:从不同的组件调用方法
这是第一个组件:在sendCardComponent定义
import { Component, OnInit, Inject } from '@angular/core';
import { Http } from '@angular/http';
import { SendCardComponent } from '../send-card/send-card.component';
@Component({
selector: 'app-input-field',
templateUrl: './input-field.component.html',
styleUrls: ['./input-field.component.css'],
})
export class InputFieldComponent implements OnInit {
value = '';
output = '';
@Inject(SendCardComponent) saro: SendCardComponent;
constructor(private http : Http) { }
onEnter(value: string) {
this.value = value;
this.http.post('http://localhost:5000/APIconversation/', {"val":value})
.map(response=>response.json())
.subscribe(
data => {
this.output = data.result.fulfillment.speech,
if(data.result.fulfillment.speech == 'test'){
saro.sendCard('done', '1');
}
});
}
我想打电话给sendCard(),从InputFieldComponent它看起来像这样:
import { Component, OnInit } from '@angular/core';
import { Http } from '@angular/http';
@Component({
selector: 'app-send-card',
templateUrl: './send-card.component.html',
styleUrls: ['./send-card.component.css']
})
export class SendCardComponent implements OnInit {
constructor(private http : Http) { }
ngOnInit() {
}
output = '';
sendCard(value:string, id:number){
this.http.post('http://localhost:5000/APIconversation/', {"val":value})
.map(response=>response.json())
.subscribe(
data => {
this.output = data.result.fulfillment.messages[1].payload.options[id].type = $('#'+(id+1)+'>span').html();
});
} //sendCard
}
打电话时我得到一个错误saro.sendCard:
[ts]找不到名字'saro'
我在做什么错?
你需要使用this.saro' – snorkpete