app.js
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
return console.log(error);
}
console.log('Message sent: %s', info.messageId);
// Preview only available when sending through an Ethereal account
console.log('Preview URL: %s', nodemailer.getTestMessageUrl(info));
res.render('message',{msg:'email has been sent'});
});
component.html
<form #userForm="ngForm" (ngSubmit) = "onSubmit(userForm.value)">
<div class="form-group">
<label for="exampleInputPassword1">Name</label>
<input type="text" class="form-control" placeholder="Enter Name" name="who" ngModel>
<label for="exampleInputPassword1">Email</label>
<input type="email" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Enter email" name="email" ngModel>
<small id="emailHelp" class="form-text text-muted">I'll never share your email with anyone else.</small><br>
<input type="text" class="form-control" placeholder="Type here..." name="message" ngModel>
</div>
<button type="submit" class="btn btn-primary">Sent Mail</button>
</form>
{{msg}}
component.ts
export class MessageComponent implements OnInit {
constructor( private http:Http) { }
ngOnInit() {
}
onSubmit = function (user) {
console.log(user);
//this.http.post('http://xxx/externalapi/add', user);
let parm=JSON.stringify(user);
console.log(parm);
let headers = new Headers({"Content-Type": "application/json"});
let options = new RequestOptions({headers: headers});
this.http.post("/api/send", parm, options).subscribe(data => {
});
}
我想通过电子邮件使用angular4发送消息。我可以成功发送。后 我想从app.js向客户端发送成功的消息但是我 不知道我该怎么办? 我想将值发送到我的component.html页面中的{msg}变量
答案 0 :(得分:0)
在你的app.js文件中,你不需要res.render。您没有呈现页面。您需要发送的只是一些快速的JSON数据。所以,而不是res.render()
。使用res.json()
See here
接下来,您需要做的就是在组件中侦听响应并处理它。
this.http.post("/api/send", parm, options).subscribe(data => {
this.msg = data.message; // Or Handle however you want. You should also handle errors
});
答案 1 :(得分:0)
谢谢。我解决了我用它来发送回复。
res.json({“msg”:“message sent”});
我使用
获取回复.map(res=>this.mesg =res.json())