简体   繁体   English

如何在angular5中使用jquery .counterUp函数?

[英]How to use jquery .counterUp function in angular5?

I want to create a counter up view for my company website, to do this i have to use jquery counterUp function. 我想为我的公司网站创建一个计数器视图,要执行此操作,我必须使用jquery counterUp函数。

Angular is not recognising the function even after adding script tags for it. 即使为它添加了脚本标签,Angular也无法识别该功能。

if(st >= hT ){
  $('.counter').counterUp({
    delay: 10,
    time: 2000
  });

The counter should start from zero and reach a certain defined number. 计数器应从零开始并达到确定的数字。

Assuming that the element referenced by ".counter" is an Angular component you should be using ViewChild instead of jQuery. 假设“ .counter”引用的元素是Angular组件,则应使用ViewChild而不是jQuery。

For example if the element was of class Counter then the likely better way to achieve this affect would be. 例如,如果该元素属于Counter类,则可能会获得更好的效果。

@ViewChild(Counter)
counter: Counter;

Then in the code block it would look like this. 然后在代码块中,它看起来像这样。

if(st >= hT ){
  this.counter.counterUp({
    delay: 10,
    time: 2000
});

1./ Install jQuery 1. /安装jQuery

npm install jquery npm安装jQuery

2./ Open angular.json and find scripts 2. /打开angular.json并找到脚本

  "scripts": [
  "node_modules/jquery/dist/jquery.min.js" ]

3./ go to your component and on top declare this 3. /转到您的组件,并在顶部声明此

declare var jQuery: any;

4./ and in side ngOnInit 4. /和ngOnInit内

  (function ($) {
     if(st >= hT ){
         $('.counter').counterUp({
           delay: 10,
           time: 2000
         });
  })(jQuery); 

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

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