2

I have recently started working on an Angular 2 with Bootstrap project. I want to add bootstrap-toggle checkbox to UI. I downloaded and followed instruction on this site. It doesn't show the check box as toggled, but it shows it as normal check box. The Index.html is :

<!DOCTYPE html>
<html>

<head>
  <meta http-equiv="X-UA-Compatible" content="IE=Edge">

  <!--[if lt IE 9]>
          <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
        <![endif]-->
  <script>
    document.write('<base href="' + document.location + '" />');
  </script>

  <script src="node_modules/jquery/dist/jquery.js"></script>

  <!-- Bootstrap JS dependencies-->
  <link rel="stylesheet" href="content/css/bootstrap.min.css" />
  <link rel="stylesheet" href="content/css/font-awesome.min.css">
  <link rel="stylesheet" href="content/css/bootstrap-toggle.min.css" />
  <link rel="stylesheet" href="content/css/app-styles.css" />
  <script src="content/js/bootstrap.min.js"></script>


  <!-- 1. Load libraries -->
  <!-- Polyfill(s) for older browsers -->
  <script src="node_modules/core-js/client/shim.min.js"></script>
  <script src="node_modules/zone.js/dist/zone.js"></script>
  <script src="node_modules/reflect-metadata/Reflect.js"></script>
  <script src="node_modules/systemjs/dist/system.src.js"></script>

  <!-- 2. Configure SystemJS -->
  <script src="systemjs.config.js"></script>


  <script>
    System.import('app').catch(
      function(err) {
        console.error(err);
      });

    $(function() {
      $('#showOpen').bootstrapToggle();
    })
  </script>

</head>

<body>

  <my-web-app>Loading ...</my-web-app>

  <script src="content/js/bootstrap-toggle.js"></script>

</body>


</html>

code snippet

Here, my-web-app calls an Angular 2 component which creates a form. The form has checkbox as

<div class="form-group">
  <label for="showOpen" class="col-md-4 control-label">Show Open :</label>
  <div class="col-md-1 checkbox">
    <input [(ngModel)]="model.showOpen" name="showOpen" type="checkbox" id="showOpen" data-toggle="toggle" checked class="control-form">
  </div>
</div>

This is not showing checkbox as Toggle checkbox. The checkbox is shown as normal HTML checkbox.

As you can see I have also added the initialization code in index.html too.

But if I add toggle checkbox as first element (before my-web-app) in the Index.html file, it start showing correctly. Not sure what I am missing. Could you please help.

mehta
  • 735
  • 1
  • 11
  • 26

1 Answers1

1

Doing DOM selection via the $ inside an angular app is not recommended.

See here How to use jQuery with Angular2? for a better solution if you really need to use jQuery.

Look at http://angularjs.blogspot.ca/2016/04/5-rookie-mistakes-to-avoid-with-angular.html as well... though it is dated still offers you a breadth for the direction you need to take.

Community
  • 1
  • 1
Nico
  • 1,961
  • 17
  • 20