【问题标题】:ng-repeat is not working when directive is called调用指令时 ng-repeat 不起作用
【发布时间】:2015-08-07 08:29:07
【问题描述】:

我使用 BootGrid 数据表和 JSON 文件作为表的数据源。

服务

.service('datatableService', ['$resource', function($resource){
        this.getDatatable = function(id, email, date) {
            var datatableList = $resource("data/data-table.json");

            return datatableList.get ({
                id: id,
                email: email,
                date: date
            })
        }
    }])

控制器

.controller('datatableCtrl', function($scope, datatableService){

        //Get Data Table Data
        $scope.id = datatableService.id;
        $scope.email = datatableService.email;
        $scope.date = datatableService.date;

        $scope.dtResult = datatableService.getDatatable($scope.id, $scope.email, $scope.date);


    })

指令

.directive('bootgrid', ['$timeout', function($timeout){
    return {
        restrict: 'A',
        link: function(scope, element, attr){
            $('#data-table-basic').bootgrid({
                css: {
                    icon: 'md icon',
                    iconColumns: 'md-view-module',
                    iconDown: 'md-expand-more',
                    iconRefresh: 'md-refresh',
                    iconUp: 'md-expand-less'
                }

            });
        }   
    }
}])

HTML

<div class="table-responsive" data-ng-controller="datatableCtrl">
        <table id="data-table-basic" class="table table-striped" data-bootgrid>
            <thead>
                <tr>
                    <th data-column-id="id" data-type="numeric">ID</th>
                    <th data-column-id="sender">Sender</th>
                    <th data-column-id="received" data-order="desc">Received</th>
                </tr>
            </thead>
            <tbody>
                <tr data-ng-repeat="w in dtResult.list">
                    <td>{{ w.id }}</td>
                    <td>{{ w.email }}</td>
                    <td>{{ w.date }}</td>
                </tr>
            </tbody>
        </table>
    </div>

当我运行它时,&lt;tbody&gt; 内没有数据,但是当我删除指令时,我可以看到所有 JSON 数据都呈现在表中。我希望 ng-repeat 和 and 指令一起工作。我试图将指令中的优先级设置为,

...
   return {
        restrict: 'A',
        priority: 1001,
        ...

但没有运气。 http://plnkr.co/edit/rWCVXTjxOGZ49CeyIn9d?p=preview

请帮我解决这个问题。如果你能修好上面的笔,我会很合适。

问候

【问题讨论】:

    标签: javascript angularjs angularjs-directive jquery-bootgrid


    【解决方案1】:

    Priority 设置在这里没有帮助,因为它用于调节在同一元素上定义的指令的编译顺序。

    您可以使用$timeout 服务将bootgrid 指令初始化延迟到下一个摘要周期。您还需要注意数据对象的变化,因为您使用 AJAX 加载它。

    app.directive('bootgrid', function($timeout) {
        return {
            link: function(scope, element, attr) {
                scope.$watch(attr.bootgrid + '.length', function(newVal, oldVal) {
                    if (newVal !== oldVal && newVal) {
                        $timeout(function() {
                            element.bootgrid({
                                css: {
                                    icon: 'md icon',
                                    iconColumns: 'md-view-module',
                                    iconDown: 'md-expand-more',
                                    iconRefresh: 'md-refresh',
                                    iconUp: 'md-expand-less'
                                }
                            });
                        });
                    }
                });
            }
        }
    });
    

    演示: http://plnkr.co/edit/ehbzoFGOqhQedchKv0ls?p=preview

    【讨论】:

    • 感谢您的回复。当我使用本地数据时,您的方法有效。当像我上面的示例那样从 JSON 文件调用数据时,它不起作用。
    • 这很酷。我有2个疑问。这意味着,scope.$watch(attr.bootgrid + '.length', function(newVal, oldVal) { 您是否在此处查找属性的任何更改,
    • 您能否解释一下您在此处添加的额外代码 datatableService.getDatatable($scope.id, $scope.email, $scope.date).$promise.then(function(data ) { this.datalist = data; }.bind(this));
    • 不,它监视数据数组长度的变化。最初它是未定义的,因为数组未定义,当它被加载时它会有所不同。 Angular 在范围上下文中评估提供给$watch 的字符串的值。
    • 数据绑定工作不正常。如果 datalist 稍后更改,网格不会反映更改
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-14
    • 1970-01-01
    • 2015-07-26
    • 1970-01-01
    相关资源
    最近更新 更多