【问题标题】:popover with ng-repeat data not showing after ng-clickng-click 后未显示 ng-repeat 数据的弹出窗口
【发布时间】:2020-06-08 01:40:27
【问题描述】:

我有一个按钮来显示从服务器加载的数据的弹出框:

<button ng-click="main.getFieldDescriptions()" data-placement="bottom"
        data-toggle="popover" data-container="body"/>

这是一个我想显示为弹出框内容的元素:

<div id="field-descriptions" class="hide" style="width:500px">
    <div class="row" ng-repeat="f in main.fieldDescriptions">
        <input class="form-control" readonly="true" value="{{f.fieldName}}" />
    </div>
</div>

数据来自服务器并带有获取休息请求(这是我在控制器中的内容)

angular.element("#show-fields-description").popover({
    html: true,
    content: function () {
        return angular.element("#field-descriptions").html();
    }
});

this.getFieldDescriptions = function getFieldDescriptions() {
    if (self.report.sysparm_table) {
        return server.get(url)
            .then(function getData(response) {
                self.fieldDescriptions = response.result;
            });
    }
    return null;
};

单击按钮时弹出框显示空白内容,第二次单击时显示带有数据的弹出框。似乎popover首先显示,然后加载服务器值。

【问题讨论】:

    标签: jquery angularjs twitter-bootstrap popover


    【解决方案1】:

    您正在将平面引导弹出窗口与 angularjs 混合......所以 angularjs 的 $apply/digest 循环不知道它。

    有很多方法。

    1. 模态加载时调用 $apply

    2. 在 ng-click 回调中调用 .popover('show') 而不是 html 上的数据切换

    3. 使用 Angular ui 引导弹出框

    对于第一个:

    $('#show-fields-description').on('shown.bs.popover', function () {
      main.getFieldDescriptions();
    
      //Need to call $scope.$apply() after data is there
    })
    

    第二个:

    请看这个https://getbootstrap.com/docs/4.3/components/popovers/#options

    从 html 中删除所有其他属性:

    <button ng-click="main.getFieldDescriptions()"></button>
    
    angular.element("#show-fields-description").popover({
        html: true,
        content: function () {
            return angular.element("#field-descriptions").html();
        },
        container: 'body',
        trigger: 'manual',
        placement: 'bottom'
    });
    

    然后在此处调用 show/toggle:

    this.getFieldDescriptions = function getFieldDescriptions() {
        if (self.report.sysparm_table) {
            return server.get(url)
            .then(function getData(response) {
                self.fieldDescriptions = response.result;
                angular.element("#show-fields-description").popover('show');
            });
        }
        return null;
    };
    

    【讨论】:

    • 我认为第二种解决方案在我的情况下是最好的,您能否添加更多有关它的详细信息
    • 我使用了第二种方法,它再次显示空弹出窗口,然后单击它显示数据。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多