javascript - 如何在另一个函数中使用来自 ngOnInit() 的定义变量?
问题描述
我是 Angular 5 的新手。我正在使用 jquery Rate Yo 插件在 Angular 5 网站中进行评级。我在其中调用了评级插件ngOnInit()
,它工作正常。点击星的值存储在this.amb
, this.clean
, this.serv
, 中this.comf
。这些所有值都显示在控制台中。我想将此值与API
提交按钮上的我一起使用,但这些变量的值未定义。那么如何在我的onSubmit()
功能上获得这些价值。?请帮忙。
ngOnInit() {
$(function () {
$("#ambience").rateYo({
starWidth: "18px",
fullStar: true,
halfStar: false,
onSet: function (rating, rateYoInstance) {
this.amb = rating;
console.log(this.amb);
},
});
});
$(function () {
$("#serviceq").rateYo({
starWidth: "18px",
fullStar: true,
halfStar: false,
onSet: function (rating, rateYoInstance) {
this.serv = rating;
console.log(this.serv);
},
});
});
$(function () {
$("#clean").rateYo({
starWidth: "18px",
fullStar: true,
halfStar: false,
onSet: function (rating, rateYoInstance) {
this.clean = rating;
console.log(this.clean);
},
});
});
$(function () {
$("#comfort").rateYo({
starWidth: "18px",
fullStar: true,
halfStar: false,
onSet: function (rating, rateYoInstance) {
this.comf = rating;
console.log(this.comf);
},
});
});
}
onSubmit() {
this.common.createAPIService('api/contact/Feedback?FirstName=' + this.formGroup.controls.FirstName.value + '&LastName=' + this.formGroup.controls.LastName.value + '&Address=' + this.formGroup.controls.Address.value + '&City=' + this.formGroup.controls.City.value + '&Cinema=' + this.formGroup.controls.Cinema.value + '&Pincode=' + this.formGroup.controls.Picode.value + '&Phone=' + this.formGroup.controls.Mobile.value + '&Email=' + this.formGroup.controls.Email.value + '&Comments=' + this.formGroup.controls.Comments.value + '&ServiceQuality='+ this.serv +'&Ambience=' + this.amb +'&Cleanliness='+ this.clean + '&Comfort='+ this.comf,'').subscribe((result: any) => {
//alert(result.Message);
this.common.ShowNotification("FeedBack", result.Message, "info");
this.onReset();
})
}
解决方案
这是因为您使用的是传统函数而不是箭头函数
使用箭头功能
ngOnInit() {
$(() => {
$("#ambience").rateYo({
starWidth: "18px",
fullStar: true,
halfStar: false,
onSet: function (rating, rateYoInstance) {
this.amb = rating;
console.log(this.amb);
},
});
});
$(() => {
$("#serviceq").rateYo({
starWidth: "18px",
fullStar: true,
halfStar: false,
onSet: function (rating, rateYoInstance) {
this.serv = rating;
console.log(this.serv);
},
});
});
$(() => {
$("#clean").rateYo({
starWidth: "18px",
fullStar: true,
halfStar: false,
onSet: function (rating, rateYoInstance) {
this.clean = rating;
console.log(this.clean);
},
});
});
$(() => {
$("#comfort").rateYo({
starWidth: "18px",
fullStar: true,
halfStar: false,
onSet: function (rating, rateYoInstance) {
this.comf = rating;
console.log(this.comf);
},
});
});
}
onSubmit() {
this.common.createAPIService('api/contact/Feedback?FirstName=' + this.formGroup.controls.FirstName.value + '&LastName=' + this.formGroup.controls.LastName.value + '&Address=' + this.formGroup.controls.Address.value + '&City=' + this.formGroup.controls.City.value + '&Cinema=' + this.formGroup.controls.Cinema.value + '&Pincode=' + this.formGroup.controls.Picode.value + '&Phone=' + this.formGroup.controls.Mobile.value + '&Email=' + this.formGroup.controls.Email.value + '&Comments=' + this.formGroup.controls.Comments.value + '&ServiceQuality='+ this.serv +'&Ambience=' + this.amb +'&Cleanliness='+ this.clean + '&Comfort='+ this.comf,'').subscribe((result: any) => {
//alert(result.Message);
this.common.ShowNotification("FeedBack", result.Message, "info");
this.onReset();
})
}
对于一个好的评级组件:https ://primefaces.org/primeng/#/rating
推荐阅读
- firebase - FirebaseError:无法对根引用执行“put”操作,请使用子级创建非根引用,例如 .child('file.png')
- php - 将会话插入 Mysql
- django - Django IntegrityError NOT NULL 约束失败,不使用 null 和空白
- c# - c# SourceGenerator 忽略添加到生成代码的 xml 注释
- python - “WebElement”类型的对象没有 len() - Python Selenium
- list - Jinja显示最大的数字
- ckeditor - 如何在ckeditor中获取提到的用户项目对象
- javascript - 下一条语句不会捕获更改的 css() 值。为什么?
- angular - 如何在角度 api 调用中传递参数
- realex-payments-api - 无法初始化 Direct POST 结帐解决方案 [global-payments-api]