1

I'm new to Angular, and have been trying to implement the Bootstrap Angular toggle feature/module on my website.

There is documentation on the linked website, but full working examples (e.g. small independent fiddles) are nowhere to be found. I can't get this feature working at all, so I've loaded up the resources, and made a js-fiddle of what I've been trying:

example-fiddle

or if you prefer to just look at the code:

<script>angular.module('myApp', ['ui.toggle']);</script>

<div ng-app="myApp">
   <toggle ng-model="toggleValue" ng-change="changed()"></toggle>
</div>

Anyone familiar to Angular can probably work this out in 10 seconds, but I've just started and can't seem to find good examples to learn from.
Where am I wrong here, html call or dependency declaration (or both)?

1
  • updated the fiddle to work now, if anyone needs a simple example Commented Jun 21, 2017 at 21:05

2 Answers 2

3

I see that the cdn addresses for bootstrap toggle are referenced incorrectly in your fiddle.

I grabbed the correct addresses to reference the js and css file from chrome dev tools when on the bootstrap toggle website.

I think it's safer to download the bootstrap toggle files and reference them locally from your app.

Paste the following in your fiddle and it will work.

  

  <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
    <html>
    <link href="https://ziscloud.github.io/angular-bootstrap-toggle/css/angular-bootstrap-toggle.min.css"
                    rel="stylesheet">            
    <script src="https://ziscloud.github.io/angular-bootstrap-toggle/js/angular-bootstrap-toggle.min.js"></script>

    <script>angular.module('myApp', ['ui.toggle'])</script>
    <body ng-app="myApp">
      <div>
        <toggle ng-model="toggleValue" ng-change="changed()"></toggle>
        It's not working! What should I do?
      </div>
    </body>

    </html>

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

3 Comments

I see, I forgot to swap "{version}" in the link with the angular version that I'm using (in this case, 1.6.4). Well, thank you very much for your time, I appreciate it.
actually, this swapping may not work, but I got the point, use the appropriate resources
adding above mentioned script and stylesheet files(github ones) to my angular project V1.5.8 worked.
1

Angular Bootstrap Toggle official link http://ziscloud.github.io/angular-bootstrap-toggle/

  1. Add dependencies
  2. Add below code on view

    <toggle size="btn-sm"  ng-model="toggleValue.operationMode1" ng-click="doSwitch(toggleValue.operationMode1)" on="Auto" off="Manual" onstyle="btn-success" offstyle="btn-danger"></toggle>
    
    <toggle size="btn-sm"  ng-model="toggleValue.operationMode2" ng-click="doSwitch(toggleValue.operationMode2)" on="ON" off="OFF" onstyle="btn-success" offstyle="btn-danger"></toggle>
    
    <toggle size="btn-sm"  ng-model="toggleValue.operationMode3" ng-click="doSwitch(toggleValue.operationMode3)" on="Enable" off="Disable" onstyle="btn-success" offstyle="btn-danger"></toggle>
    
  3. Add below code on controller to custom preset toggle.

    $scope.toggleValue={
      operationMode1 :true,
      operationMode2 :false,
      operationMode3 :true
    }
    
    $scope.doSwitch = (function (toggleStatus) {
      console.log(toggleStatus);
    // Your logic here
    });
    

Comments

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.