如果没有互联网连接,我想显示吐司,不知道该怎么做
I want to display a toast if internet connection is not there, got no idea how to do that
在这里,我在提交表单时发送邮件。一切正常,唯一的问题是即使互联网连接不存在,用户也获得了成功。我希望如果互联网连接不存在,用户应该收到错误消息。
脚本
this.mail= function() {
var data = ({
name :this.name,
email :this.email
})
//Post Request
$http.post('/send', data).
success(function(response, status, headers, config){
$mdToast.show(
$mdToast.simple()
.textContent('Your form has been submitted '+data.name)
.position($scope.getToastPosition())
.hideDelay(5000)
);
}).
error(function(response, status, headers, config) {
$mdToast.show(
$mdToast.simple()
.textContent('Something went wrong, Please TRY AGAIN '+data.name)
.position($scope.getToastPosition())
.hideDelay(5000)
);
});
服务器
function send(req, res) {
console.log(req.body);
var data= req.body
smtpTransport.sendMail({
from: "<email@gmail.com>",
to: data.email,
subject: "Website Submission from "+data.name,
text: 'You have a new submission with the following details...,
}, function(error, response){ //callback
if(error){
console.log(error);
}if(error.code == "ENOENT"){
console.log("no internet connection");
}else{
console.log(" Message sent "+data.name);
}
smtpTransport.close();
});
res.json(data);
}
https://docs.angularjs.org/api/ng/service/$http$http旧承诺方法成功和错误已被弃用。请改用标准的 then 方法。如果$httpProvider.useLegacyPromiseExtensions设置为false,则这些方法将引发$http/遗留错误。
不要使用成功/错误。
你可以使用 JavaScript 代码
if(!window.navigator.onLine)
{
//you are not online
}
相关文章:
- 元素在我的代码中不会.fadeTo.Don'I don’我不知道;s错误的JavaScript、JQuery、H
- 我可以'我似乎不知道如何修复javascript中的两个lint.有人能帮我理解吗
- 在不知道深度或父属性的情况下从对象中删除属性
- Steam Web API:I'我不知道该如何处理这个看起来像哈希的url来生成图像
- 使用.on动态添加jquery/js不知道的html元素
- 这个旋钮没有更新;我不知道为什么
- 我不知道为什么我的代码是错误的?又有什么错
- 如何在不知道关键字的情况下访问javascript对象值
- 我不知道我的编码有什么问题.(JavaScript)
- 我正在尝试使用多个2数组来绑定svg圆的数据,但不知道如何绑定
- I'我不知道为什么我的代码没有'不起作用
- sqlite查询返回错误-can'我不知道为什么
- 我不知道此代码中的这些符号是什么意思.十进制到二进制
- 我不知道如何正确地将REST响应对象传递给ReactJS子组件
- 我需要迭代一个JSON数组——不知道如何做到——已经搜索过了,但仍然可以'我一点也不知道
- Expressjs不知道如何处理多个用户
- 我没有'我不知道为什么我的jqGrid子网格没有'不要给我看数据
- 如果没有互联网连接,我想显示吐司,不知道该怎么做
- 不知道为什么这个方法会吐出它吐出的东西
- 吐司机:吐司顶部居中;t第一次点击按钮就不会被应用