2

I needed to initialize a jQuery plugin on all elements with a certain class name which comes inside an ng-repeat loop. These elements are loaded via angularjs $http get method.

var app = angular.module('myApp', []);

app.controller('myController', function($scope, $http) {
    $http.get('get_item.php').success(function(response) {
        $scope.items = response;
        angular.element('.make-switch').bootstrapSwitch(); // jQuery plugin code
    });
});

But this doesn't work. I tried $('.make-switch').bootstrapSwitch(); also. Please help.

4
  • 1
    Create a custom directive, you should not do any plugin initialization in controller. Commented Nov 4, 2015 at 7:25
  • just use $('.make-switch').bootstrapSwitch(); Provided you've included jQuery and this plugin script. Commented Nov 4, 2015 at 7:26
  • @AdityaParab OP is already doing it, can't you see? But there is a good reason why it won't work reliably. And that's the reason why it should not be part of controller responsibility. Commented Nov 4, 2015 at 7:29
  • There is a similiar post: Call jQuery function from AngularJS Controller I hope it helps you Commented Nov 4, 2015 at 7:31

3 Answers 3

8

Create a directive and so you can initialize the plugin in your markup:

<div bootstrapswitch></div>

I think you creates your elements dynamicly with the scope variable $scope.items, something like that:

<div class="make-switch" ng-repeat="item in items" bootstrapswitch></div>

...just add the attribute bootstrapswitch to initialize the switch on your loaded elements.

...your directive could look like that:

app.directive('bootstrapswitch', [function() {
   return {
      restrict: 'A',
      link: function(scope, elem, attrs) {
         $(elem).bootstrapSwitch();
      }
   };
}]);
Sign up to request clarification or add additional context in comments.

Comments

1

Actually, if you want to call any jQuery plugins inside angular code you can do this also with angular. It has built in jQuery called jqLite, and you can call this as angular.element("selector") which returns you a jQuery element. As a selector you can use any jQuery/css selectors you like.

By the way, calling jQuery directly in angular code like $("selector").method() is a bad practise. You should use only your angular jqLite functionallity

2 Comments

i used angular.element('.make-switch').bootstrapSwitch();
make sure your plugin included on webpage
1

It not a pretty good idea to manipulate view using jQuery library inside Angular controller. Actually jQuery is grabbing or injecting into your DOM based on events.But Angular doesn't act the same way, it manipulate view based on true/false nature.

Best alternative way is try to make directive or use angular-bootstrap-switch library.

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.