AngularJS ng-click在弹出框内打破

时间:2012-08-30 17:12:13

标签: twitter-bootstrap angularjs

我正在尝试编写一个指令来加载一个部分html文件,并根据一个范围&将它用作Bootstrap popover内容。

但是我坚持一个非常基本的步骤,在弹出范围内写一个hide()方法,以便我可以使用ng-click=hide()轻松关闭它。


这已经解决了&现在,掠夺者正在报道其他问题; - )。

更新:拯救者:http://plnkr.co/edit/QH3NQh?p=preview

.directive('uiPopover', ['$compile', '$http', function($compile, $http) {
return {
    restrict: 'A',
    scope: {
        hide: '&hide' // did not understand what is this
    },
    link: function postLink(scope, element, attr, ctrl) {
        console.warn('postLink', arguments, this);

        // scope is the anchor scope
        scope.name = "Hello"; // Using {{name}} is working
        scope.hide = function() { // Using ng-click="hide()" is not working :(
            console.log('in');
            element.popover('hide');
        }

        $http.get(attr.uiPopover).success(function(data) {
            element.popover({
                content: $compile(data)(scope), // popover content will get a new scope that I need to put hide() on.
                html: true
            });
        });


    }
}
}]);

4 个答案:

答案 0 :(得分:14)

我的解决方案是一样的,但无论它值多少,这都是我最终使用的代码片段。希望这有帮助!

//Initialize & config popover controls
$('[data-toggle="popover"]').popover({ html : true })
    .click(function(ev) {
     //this is workaround needed in order to make ng-click work inside of popover
     $compile($('.popover.in').contents())($scope);
});

并且不要忘记将$ compile和$ scope包含为依赖项。

答案 1 :(得分:6)

请参阅this google group thread,特别是Andy's fiddle。 难的似乎是popover小部件/组件创建了一个新的DOM元素,该元素位于ui-popover指令所在的范围之外。

答案 2 :(得分:0)

创建提示后我必须使用$compile(tip.contents())(scope);(通过绑定“点击”)。检查Plunker的解决方案。

答案 3 :(得分:0)

查看here如何满足您的需求等等。我觉得很棒)

这是图书馆Angular-Strap

的一部分
相关问题