从呼叫控制器AngularJS jQuery函数 [英] Call jQuery function from AngularJS Controller

查看:127
本文介绍了从呼叫控制器AngularJS jQuery函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个按钮下时点击节目,如通知一个小的弹出

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>

我的目的是显示这个弹出无需点击按钮每隔几秒钟,但是从AngularJS控制器。

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);
    }
};

}]);

还包括脚本

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

我看到错误的对象没有方法酥料饼

I see an error "the object has no method popover"

推荐答案

指令用于DOM操作:

<button show-notifications>

和指示

.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);
        }
    }
}]);

这篇关于从呼叫控制器AngularJS jQuery函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆