Call jQuery function from AngularJS Controller
angularjs
Solution
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);
}
}
}]);
Problem
I have a below button when on clicked shows a small popup like notification ``` <button id="element" type="button" onclick="ShowNotifications()" class="btn btn-default" data-container="body" data-toggle="popover" data-placement="bottom" data-content="Text inside popup">Notifications</button> <script type="text/javascript"> function ShowNotifications() { $('#element').popover('open'); } </script> ``` My Intention is to Show this popup every few seconds without clicking the button, but from the AngularJS Controller. ``` var showPop = function () { //how can i call that jQuery function here ?? $timeout(showPop, 1000); } $timeout(showPop, 1000); ``` Tried with the below solution ``` app.directive("showNotifications", ["$interval", function ($interval) { return { restrict: "A", link: function(scope, elem, attrs) { $interval(function () { $(elem).popover("open"); alert('hi'); }, 1000); } }; }]); ``` Also included the scripts ``` <link href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet" /> <script src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script> <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script> <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js"></script> <script src="js/app.js"></script> <script src="js/postsService.js"></script> <script src="js/directive.js"></script> <script src="js/controllers.js"></script> ``` using the directive like this ``` <button id="element" type="button" class="btn btn-default" data-container="body" data-toggle="popover" data-placement="bottom" data-content="Friend request 1" **show-notifications**>Live Notifications</button> ``` I see an error "the object has no method popover"