Call jQuery function from AngularJS Controller

后端 未结 3 1096
死守一世寂寞
死守一世寂寞 2020-12-01 02:03

I have a below button when on clicked shows a small popup like notification

相关标签:
3条回答
  • 2020-12-01 02:57

    The following steps can be followed,

    var jq = $.noConflict();
    

    then create any regular angular module and controller and create a function inside the controller which we can use it for the calling any jquery function, e.g. I want to add a class to a div element.

    angular.module('myApp',[]).controller('hello',function($scope){
                $scope.name = 'Vikash';
                $scope.cities = ['Delhi','Bokaro','Bangalore'];
    
                 $scope.hide = function(){
                    jq('#hideme').addClass('hidden');   
    
                }
            });
    

    and we will create some regular html to utilize that method with the controller.

    <body ng-controller="hello">
        <div class="container" id="hideme">
            Hello Dear
        </div>
        <button ng-click="hide()">Hide Hello</button>
    </body>
    

    Now here you can see that we are about call addClass method from the jQuery inside the function declared in the controller and part of the $scpe.

    0 讨论(0)
  • 2020-12-01 03:00

    Instead of a $ just place the key word angular

    angular.element("#id").val()
    
    0 讨论(0)
  • Directives are used for DOM manipulation:

    <button show-notifications>
    

    And the directive

    .directive("showNotifications", ["$interval", function($interval) {
        return {
            restrict: "A",
            link: function(scope, elem, attrs) {
                //On click
                $(elem).click(function() {
                    $(this).popover("open");
                });
    
                //On interval
                $interval(function() {
                    $(elem).popover("open");
                }, 1000);
            }
        }
    }]);
    
    0 讨论(0)
提交回复
热议问题