最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - How can I use JavaScript code in Angular 7? - Stack Overflow

programmeradmin9浏览0评论

I'm trying to make a collapsible navbar with the help of MaterializeCSS when used on mobile screen and need to use JavaScript code in it. Where should I write this JavaScript code?

This is the code I want to use:

**$(document).ready(function(){
    $('.sidenav').sidenav();
  });**

I'm trying to make a collapsible navbar with the help of MaterializeCSS when used on mobile screen and need to use JavaScript code in it. Where should I write this JavaScript code?

This is the code I want to use:

**$(document).ready(function(){
    $('.sidenav').sidenav();
  });**
Share Improve this question edited Mar 16, 2019 at 0:50 Peter Mortensen 31.6k22 gold badges110 silver badges133 bronze badges asked Nov 26, 2018 at 13:36 harshit raghavharshit raghav 6532 gold badges10 silver badges25 bronze badges 1
  • 1 you should write in the ponent file – brk Commented Nov 26, 2018 at 13:38
Add a ment  | 

3 Answers 3

Reset to default 10

Step-1: Created a js folder inside the assets folder.

Step-2: Created a new .js file inside the js folder. Assets -> js -> example.js

Step-3: Added path of the example.js in angular.json inside the scripts array.

Step-4: Declared the js function created in example.js inside the ponent.ts file where that function is needed.

Step-5: Made a call to the declared function inside ngOnInIt().

  1. Install jQuery npm install jquery
  2. Install MaterializeCSS npm install materialize-css@next
  3. Install types npm install --save @types/materialize-css @types/jquery
  4. Open angular.json and find scripts field
  5. Add node_modules/jquery/dist/jquery.min.js and node_modules/materialize-css/dist/js/materialize.min.js inside array:
"scripts": [
    "node_modules/jquery/dist/jquery.min.js",
    "node_modules/materialize-css/dist/js/materialize.min.js"
]
  1. Go to your ponent and add at the top declare var jQuery: any;

Example

(function ($) {
    $('.sidenav').sidenav();
})(jQuery);

You can use that inside ngOnInit.

Find and open angular.json, then add to projects.architect.build.options object next field and change filepath to your own:

"scripts": [
  "path/to/js/you/want/use.js"
]

Angular will add those custom files to result build

发布评论

评论列表(0)

  1. 暂无评论