4

我需要在 http 调用加载后运行一个运行 jPager jquery 插件的指令

  • DOM 中的元素。

    jquery 工作正常,但我无法在屏幕渲染后运行指令(它只是在之前运行,因此范围为空)

    我尝试过使用 $emit 和 $broadcast 演示,但仍然无法启动它。

    范围加载

  • 标记到 itemContainer 中,然后 jQuery 对数据进行分页。

    <div wss-pager class="holder"></div>
            <ul id="itemContainer" ng-bind-html="ctData"></ul>
    
    ////
    
    function loadData() {
                    $http({
                            method: 'GET',
                            url: 'api/getMyData',
    
                        }).
                        success(function(data, status, headers, config) {
                            // deferred.resolve(data);
                            $scope.ctData = data.m_StringValue;
                        //    
                            $scope.$emit('UpdateJPages');
                        }).
                        error(function(data, status, headers, config) {
                            alert("error" + data);
                            $scope.ctData= "";
                        });
    
                };
    
    /////////
    
      angular.module('app').directive("wssPager", [function () {
                return {
                    restrict: "A",
                    link: function (scope, elem, attrs) {
                        scope.$on('UpdateJPages', function() {
    
                            $("div.holder").jPages({
                                containerID: "itemContainer",
                                perPage: 5,
                                startPage: 1,
                                startRange: 1,
                                midRange: 5,
                                endRange: 1
                            });
    
    
     });
    
  • 4

    2 回答 2

    5

    如果ng-if 在此调用之前 cData 为空,则使用如下:

    <div wss-pager class="holder" ng-if="ctData"></div>
    

    如果没有,你可以有一个额外的变量,例如loaded

    <div wss-pager class="holder" ng-if="loaded"></div>
    function loadData() {
        $scope.loaded = false;
        $http({
            method: 'GET',
            url: 'api/getMyData'
        })
        .success(function(data, status, headers, config) {
            // deferred.resolve(data);
            $scope.ctData = data.m_StringValue;
            $scope.loaded = true
            $scope.$emit('UpdateJPages');
        })
        .error(function(data, status, headers, config) {
            alert('error' + data);
            $scope.ctData= '';
        });
    };
    
    于 2014-08-15T10:42:42.710 回答
    1

    好的,我为任何感兴趣的人找到了解决方法。可能不是最好的答案,但它已经解决了这个问题。

    我使用了 $timeout 函数来启用要渲染的屏幕。

     $timeout(function () {
           $scope.$emit('UpdateJPages');
             });
    
    于 2014-08-15T15:44:31.747 回答