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

Yichaoz

考虑以下指令:

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

  }

如果我将其应用到ng-repeat标签中,我可以注意到{{item}}直到ng-repeat结束(被阻止时alert()才进行评估在每个“循环”之后如何呈现它?

因为在实际情况下,这些项目包含一个图像,所以我想在每个图像加载后进行回调,但是如果没有img url,我将无法绑定该事件。

这是the子

潘卡·帕克(Pankaj Parkar)

我认为您是通过ng-repeatscope变量分配img src的{{x}}在这种情况下,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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章