0

I tried to add script.js to angular.json and use it in one component. That's not working. Those who suggest to add script tag to my html file thats not a good idea. Can someone suggest another idea or what I missed to make my script work?

I add my js file called script.js on angular.json

 "scripts": [
          "src/assets/js/script.js"
        ]

And I declare a variable in slider.ts

  declare const owlCarousel: any;
  ngOnInit() {
    owlCarousel();
  }

script.js

$('.owl-carousel').owlCarousel({
    loop:true,
    margin:10,
    dots:false,
    nav:true,
    mouseDrag:false,
    autoplay:true,
    animateOut: 'slideOutUp',
    responsive:{
        0:{
            items:1
        },
        600:{
            items:1
        },
        1000:{
            items:1
        }
    }
    });
        
2
  • 1
    Does this address your requirements? Commented Aug 17, 2020 at 20:37
  • I already try this method doesn't work Commented Aug 17, 2020 at 21:40

1 Answer 1

1

Try this

First add function in your script.js

export function owlCarousel() {
    $('.owl-carousel').owlCarousel({
    loop:true,
    margin:10,
    dots:false,
    nav:true,
    mouseDrag:false,
    autoplay:true,
    animateOut: 'slideOutUp',
    responsive:{
        0:{
            items:1
        },
        600:{
            items:1
        },
        1000:{
            items:1
        }
    }
    });
}

And in your component add this line to import function owlCarousel

 import {owlCarousel} from '../../assets/js/script.js' /plz add correct path 

Notice that slider.ts is not mandatory

Hope useful.

Sign up to request clarification or add additional context in comments.

2 Comments

Why the slider.ts is not mandatory !! I tried to make the script as a function to call it but still doesn't work
yeah I did & I tried sample test window.alert('hello!') but js not working I don't know why

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.