简体   繁体   English

如何在另一个函数中使用ngOnInit()中的已定义变量?

[英]How to use defined variables from ngOnInit() in another function?

I'm new to angular 5. I'm using jquery Rate Yo pluging for rating in anngular 5 website. 我是angular 5的新手,我正在使用jquery Rate Yo插件在anngular 5网站中进行评级。 where I called rating plugin in ngOnInit() and it's working fine. 我在ngOnInit()调用评级插件的ngOnInit() ,它工作正常。 The value of clicking star is storing in this.amb , this.clean , this.serv , this.comf . 单击星号的值存储在this.ambthis.cleanthis.servthis.comf These all values are showing in console. 这些所有值都显示在控制台中。 I want to use this values with my API which is on Submit button but there value of these variables getting undefined. 我想通过我的API (在“提交”按钮上)使用此值,但是这些变量的值不确定。 So how to get these value on my onSubmit() function.? 那么如何在我的onSubmit()函数上获取这些值。 Please help. 请帮忙。

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();
    })
}

It's because you use traditional functions and not arrow function 这是因为您使用的是传统功能而不是箭头功能

Using Arrow function 使用箭头功能

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();
    })
}

For a good rating component : https://primefaces.org/primeng/#/rating 要获得良好的评分组件: https : //primefaces.org/primeng/#/rating

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM