-1

I'm trying to add some Jquery Code in Angular application but showing an error because I don't know how to use that Jquery code in the angular if I running in HTML file working fine below is my sample code please help me

<script type="text/javascript">
    $('.js-example-basic-single').select2({
        placeholder: '--- Select Country ---'
    });
</script>
Deepak
  • 21
  • 1
  • 5

3 Answers3

0

You simply need to add the CDN link of jquery in the tag. You can find the CDN links for each version of jquery here.

assax24
  • 171
  • 5
0

add this to your code before using jquery

<script
  src="https://code.jquery.com/jquery-3.4.1.min.js"
  integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="
  crossorigin="anonymous"></script>
Andrew Nic
  • 108
  • 1
  • 11
0

var appname = angular.module('appname', []);
appname.controller('appCtrl', ['$scope',
  function($scope) {
    $scope.greeting = {
      text: 'Hello'
    };
  }
]);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.20/angular.min.js"></script>
<script src="https://rawgit.com/select2/select2/master/dist/js/select2.js"></script>
<link href="https://rawgit.com/select2/select2/master/dist/css/select2.min.css" rel="stylesheet" />
<link href="style.css" rel="stylesheet" />
<script src="script.js"></script>

<body ng-app="appname">
  <div ng-controller="appCtrl">
    <p>{{greeting.text}}, world </p>
  </div>
  <select id="example" class='js-example-basic-single' multiple="multiple" style="width: 300px">
    <option value="JAN">January</option>
    <option value="FEB">February</option>
    <option value="MAR">March</option>
    <option value="APR">April</option>
    <option value="MAY">May</option>
    <option value="JUN">June</option>
    <option value="JUL">July</option>
    <option value="AUG">August</option>
    <option value="SEP">September</option>
    <option value="OCT">October</option>
    <option value="NOV">November</option>
    <option value="DEC">December</option>
  </select>
  <script type="text/javascript">
    $('.js-example-basic-single').select2({
      placeholder: 'Select a month'
    });
  </script>

Note:-This is the simple demo to for your query You have to add the CDN link of j query.

Parth Raval
  • 4,097
  • 3
  • 23
  • 36