加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

从AngularJS控制器调用jQuery函数

发布时间:2020-12-17 08:04:46 所属栏目:安全 来源:网络整理
导读:当点击时,我有一个下面的按钮显示一个小的弹出窗口,如通知 button id="element" type="button" onclick="ShowNotifications()" class="btn btn-default" data-container="body" data-toggle="popover" data-placement="bottom" data-content="Text inside
当点击时,我有一个下面的按钮显示一个小的弹出窗口,如通知
<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>

我的意图是每隔几秒钟显示此弹出窗口,而不需要单击按钮,而是从AngularJS控制器。

var showPop = function () {
    //how can i call that jQuery function here ??
    $timeout(showPop,1000);
}
$timeout(showPop,1000);

试用以下解决方案

app.directive("showNotifications",["$interval",function ($interval) {
    return {
        restrict: "A",link: function(scope,elem,attrs) {          
            $interval(function () {
                $(elem).popover("open");
                alert('hi');
            },1000);
        }
    };
}]);

还包括脚本

<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>

使用这样的指令

<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>

我看到一个错误“对象没有方法popover”

指令用于DOM操作:
<button show-notifications>

和指令

.directive("showNotifications",function($interval) {
    return {
        restrict: "A",attrs) {
            //On click
            $(elem).click(function() {
                $(this).popover("open");
            });

            //On interval
            $interval(function() {
                $(elem).popover("open");
            },1000);
        }
    }
}]);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读