【问题标题】:Angular, firing event in nested repeat on a $lastAngular,在 $last 上的嵌套重复中触发事件
【发布时间】:2015-06-02 01:09:26
【问题描述】:

所以我有一个嵌套的 ng-repeat,我试图在其中触发一个事件。所以我有类似的东西

  <div ng-repeat="item in items">
     <div ng-repeat="prop in item.props"> {{prop.name}}</div>
  </div>

所以我希望在最后一个项目中的最后一个道具被渲染后触发一个事件。

所以我的第一个想法是把逻辑放在一个带有 restrict="C" 的指令中,然后做一些类似

的事情
 ng-class=“{ fix-directive : prop.$parent.$last && prop.$last}”

其中 fix-directive 是具有我想要触发的逻辑的指令。这样的事情可能吗?或者也许有更好的方法?谢谢!

【问题讨论】:

    标签: javascript angularjs


    【解决方案1】:

    如果你想在最后一个元素被渲染后触发指令,你必须在指令中添加一个条件;

        .directive('repeat-element',['$timeout','$http',function($timeout,$http) {
                            return {
                                restrict : 'A',
                                link : function(scope, element, attrs) {
                                    if (scope.$last === true) {
                                        $timeout(function() {
                                            //timeout so that it is called when everything gets rendered
                                            doStuff(scope.items);
                                        });
                                    }
                                    //rest of the code
    
    ....................
    

    我就是这样做的,所以您可以添加更多条件,例如 parent.$last 也是如此。

    这应该做你的工作

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-11-18
      • 2019-08-15
      • 1970-01-01
      • 2011-02-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-16
      相关资源
      最近更新 更多