如何在ng-repeat期间呈现{{}}?

时间:2015-08-27 19:50:31

标签: javascript angularjs events dom angularjs-directive

考虑以下指令:

Intent intent = new Intent(getBaseContext(), SignoutActivity.class);
intent.putExtra("EXTRA_SESSION_ID", sessionId);
startActivity(intent)

如果我将它应用到ng-repeat标签中,我会注意到{ng_cart}}在ng-repeat结束之前(被.directive('otherDirective', function(){ return { link: function(scope, elem, attr){ console.log(elem[0]); alert(elem[0]); } } 阻止时)不会被评估。如何在每个"循环"?

之后渲染它

因为在实际场景中,这些项目包含图像,我想在每个图像加载后进行回调,但是如果没有img url,我就无法绑定事件。

here is the plunker

2 个答案:

答案 0 :(得分:3)

使用$timeout等待ng-repeat完成:Plunker

.directive('otherDirective',['$timeout', function($timeout){
  return {
    link: function(scope, elem, attr){
      $timeout(function () {
        console.log(elem[0]);
        alert(elem[0]);
      });
    }
  };
}]);

答案 1 :(得分:1)

我认为您是通过ng-repeat范围变量{{x}}分配img src。在这种情况下,如果你真的感兴趣直到加载了图像元素,你可以简单地放置onload事件。我建议你使用on('load', funnction(){ //code })事件。只要图像加载到DOM,此事件就会调用函数。

<强>代码

.directive('otherDirective', function() {
    return {
      link: function(scope, elem, attr) {
        //element before loading
        console.log(elem[0]);
        elem.find('img').on('load', function() {
          //element after loaded
          console.log(elem[0]);
          //if you are going to manipulate scope binding here
          //then you have to run digest cycle manually
          //as this event will considered as outside of angular context
          //after binding/DOM manipulation is done..do apply digest by doing scope.$apply()
        })
      }
    }
});

Demo Plunkr