我有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