简体   繁体   English

在我的Angular项目中使用一些jquery代码

[英]Using some jquery code in my Angular project

I am using Angular 8, I have some code that I would like to add to my app.component.ts The code already works on a standalone html file that I've been testing it in but I would now like to transfer it to my angular component. 我正在使用Angular 8,我想将一些代码添加到我的app.component.ts中,这些代码已经可以在我已经对其进行测试的独立html文件中运行,但是现在我想将其传输到我的角度分量。

Here is the code: 这是代码:

<script>
$(function() {

  var $input = $("input[type='search']"),
  $clearBtn = $("button[data-search='clear']"),
  $prevBtn = $("button[data-search='prev']"),
  $nextBtn = $("button[data-search='next']"),
  $content = $(".content"),
  $results,
  currentClass = "current",
  offsetTop = 50,
  currentIndex = 0;

  /**
   * Jumps to the element matching the currentIndex
   */
  function jumpTo() {
    if ($results.length) {
      var position,
        $current = $results.eq(currentIndex);
      $results.removeClass(currentClass);
      if ($current.length) {
        $current.addClass(currentClass);
        position = $current.offset().top - offsetTop;
        window.scrollTo(0, position);
      }
    }
  }

  /**
   * Searches for the entered keyword in the
   * specified context on input
   */
  $input.on("input", function() {
    var searchVal = this.value;
    $content.unmark({
      done: function() {
        $content.mark(searchVal, {
          separateWordSearch: true,
          done: function() {
            $results = $content.find("mark");
            currentIndex = 0;
            jumpTo();
          }
        });
      }
    });
  });

  /**
   * Clears the search
   */
  $clearBtn.on("click", function() {
    $content.unmark();
    $input.val("").focus();
  });

  /**
   * Next and previous search jump to
   */
  $nextBtn.add($prevBtn).on("click", function() {
    if ($results.length) {
      currentIndex += $(this).is($prevBtn) ? -1 : 1;
      if (currentIndex < 0) {
        currentIndex = $results.length - 1;
      }
      if (currentIndex > $results.length - 1) {
        currentIndex = 0;
      }
      jumpTo();
    }
  });
});

</script>   

My angular page looks like this: 我的角度页面如下所示:

import { Component } from '@angular/core';
declare var $: any;

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent  {

}

How can I use the code inside my angular? 我该如何在我的角度内使用代码? p:s: I can already use jquery in the Angular project p:s:我已经可以在Angular项目中使用jquery

Step 1. Make sure you have jQuery mentioned in the scripts section of the angular-cli.json 步骤1.确保您在angular-cli.json的脚本部分中提到了jQuery。

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

Step 2. In your app.component.ts right after import { Component, OnInit } from '@angular/core'; 步骤2. import { Component, OnInit } from '@angular/core';之后,立即在app.component.ts中import { Component, OnInit } from '@angular/core'; add declare var $:any; 添加declare var $:any; like this: 像这样:

import { Component, OnInit } from '@angular/core';
declare var $:any;

Step 3. Although not compulsory, but I find it safer to put jQuery and JS code inside ngOnInit() like this: 第3步。虽然不是强制性的,但我发现将jQuery和JS代码放在ngOnInit()更安全:

ngOnInit() {
    $('.carousel').carousel({
        interval: 2000
    });
}

I hope this works. 我希望这行得通。

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

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