【问题标题】:Load directive after AJAX callAJAX 调用后加载指令
【发布时间】:2015-04-07 22:25:13
【问题描述】:

我已经建立了一个分页指令,它需要两个参数;当前页面和总页数。

<pagination page="page" number-of-pages="numberOfPages"></pagination>

问题是我只会在 AJAX 调用(通过 ng-resource)之后知道 numberOfPages 的值。但是我的指令在 AJAX 调用完成之前已经呈现。

app.controller('MyController', function ($scope, $routeParams) {
    $scope.page = +$routeParams.page || 1,
    $scope.numberOfPages = 23; // This will work just fine

    MyResource.query({
        "page": $scope.page,
        "per_page": 100
    }, function (response) {
        //This won't work since the directive is already rendered
        $scope.numberOfPages = response.meta.number_of_pages;
    });
});

我更喜欢等待控制器模板的呈现,直到 AJAX 调用完成。

B 计划是在 AJAX 调用完成时将模板附加到指令模板中。

我无法解决这两种情况。

【问题讨论】:

    标签: angularjs angularjs-directive angularjs-resource


    【解决方案1】:

    如果您使用ui-router 中的resolve,您可以在视图渲染之前将metameta.number_of_pages 注入控制器。

    //routes.js
    angular.module('app')
      .state('some_route', {
        url: '/some_route',
        controller: 'MyController',
        resolve: {
          meta: ['MyResource', function (MyResource) {
            return MyResource.query({
              "page": $scope.page,
              "per_page": 100
            }, function (response) {
              return response.meta;
            });
          }]
        }
      });
    
    //controllers.js
    app.controller('MyController', function ($scope, $routeParams, meta) {
      $scope.page = +$routeParams.page || 1,
        $scope.numberOfPages = meta.number_of_pages;
    });
    

    【讨论】:

      【解决方案2】:

      但是在所有完成之前是否可以阻止渲染

      • 我认为 ng-if 会这样做,而 ng-show/ng-hide 只会改变实际显示

      【讨论】:

      • 这也有效:)。但我更喜欢将延迟渲染逻辑保留在指令的 JS 代码中(而不是 HTML)。
      • ng-if 是一个更好的解决方案,因为它应该是指令的所有者(即 html)来决定是否加载它
      • 另一个提醒,在 html 元素上使用 ng-if 比添加 scope.$watch 函数更容易。我遇到了同样的问题,即从 ajax 加载大型数据集并在渲染指令之前需要它。感谢您的提示!
      • 这个答案就是我想要的。太好了!
      【解决方案3】:

      您必须使用 $watch 函数等待值,例如:

      <div before-today="old" watch-me="numberOfPages" >{{exampleDate}}</div>
      

      指令

      angular.module('myApp').directive('myPagingDirective', [
        function () {
          return {
              restrict: 'A',   
            link: function (scope, element, attr) {
      
      
              scope.$watch(attr.watchMe,function(newValue,oldValue){
                      //check new value to be what you expect.
                   if (newValue){           
                        // your code goes here
                   }
              });
            } 
          };
        }
      ]);
      

      重要提示:您的指令可能使用隔离范围,但即使如此,原则也是如此。

      【讨论】:

      • 谢谢。我不知道链接(而不是控制器)。我认为可以使用 ng-hide 隐藏指令并在值更改时显示它。但是难道不能在一切都完成之前阻止渲染吗?
      • 还有;我让手表功能正常工作。但是一旦检测到新的变化,我该如何重新渲染指令呢?
      • 你能举例说明如何处理孤立的作用域吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-08-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-25
      相关资源
      最近更新 更多