首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用于更改属性的Angular指令通信

用于更改属性的Angular指令通信
EN

Stack Overflow用户
提问于 2015-03-04 02:37:02
回答 1查看 67关注 0票数 0

I有两个指令,它们的功能是在页面加载时选择随机的背景图像。

代码语言:javascript
复制
.directive('carousel', function(){
return{
  templateUrl: "../templates/carousel.html",
  restrict: "E",
  controller: function($scope, $route) {
    $scope.imageLevel = 1;
    angular.element(document).ready(function() {
      var random = Math.floor((Math.random() * 4));
      $scope.imageLevel = random;
      console.log(random);
      });
    }

 }            
})

.directive('stateDisplay', function(){
  return {
    link: function(scope, el, attrs) {
      var parms = attrs['stateDisplay'].split(' ');
      var linkVar = parms[0];
      var classes = parms.slice(1);
      scope.$watch(linkVar, function(newVal) {
          el.removeClass(classes.join(' '));
          el.addClass(classes[newVal]);
      });  
    }
  }

});

Carousel.html

代码语言:javascript
复制
<article class="fullheight" state-display="imageLevel image1 image2 image3 image4">            


</article> 

这些都在一起运行得很好。我想添加一个属性指令来根据滚动位置来偏移图像背景。我有一个属性,但是不确定如何获得css属性的句柄,这个css属性是随机选择并附加到dom的。

这是我到目前为止所知道的:

代码语言:javascript
复制
app.directive('scrollPosition', function($window) {
return {
    scope: {
      scroll: '=scrollPosition'
    },
    controller: function($scope) {
  $scope.scroll = 0;
},
    link: function(scope, element, attrs) {
      var windowEl = angular.element($window);
      var handler = function() {
        scope.scroll = windowEl.scrollTop();
        console.log(element[0].attrs);
      }
      windowEl.on('scroll', scope.$apply.bind(scope, handler));
      handler();
    }
  };
});

下面是html:

代码语言:javascript
复制
<article class="fullheight" scroll-position="scroll" state-display="imageLevel image1 image2 image3 image4">            

编译时的HTML:

代码语言:javascript
复制
<carousel class="ng-isolate-scope">
<article  scroll-position="scroll" class="fullheight ng-isolate-scope image2" state-display="imageLevel image1 image2 image3 image4">
</artice>
</carousel>

我希望获得"image2“css类,或者任意放置在那里的样式,并在处理函数中调整该类css。获得随机选择的类的最佳方法是什么?

代码语言:javascript
复制
console.log(attrs.class);

当前仅返回"fullheight“

EN

回答 1

Stack Overflow用户

发布于 2015-03-04 04:19:32

我最终弄明白了:这是最终的代码……

代码语言:javascript
复制
app.directive('scrollPosition', function($window) {


return {
    scope: {
      scroll: '=scrollPosition'
    },
    controller: function($scope) {
  $scope.scroll = 0;
},
  compile: function compile(tElement, tAttrs, transclude) {  
    return{
    post: function(scope, element, attrs) {
      var windowEl = angular.element($window);

  var lastScrollTop = 0;
  var handler = function() {
    scope.scroll = windowEl.scrollTop()/5;
    var myObject = attrs.$$element[0].classList[2];
    console.log(scope.scroll + " " + lastScrollTop);

    if(scope.scroll > lastScrollTop){
    $('.fullheight.' + myObject).css({backgroundPositionY:"+" + scope.scroll +"px"});
    }else{
    $('.fullheight.' + myObject).css({backgroundPositionY:"+" + scope.scroll + "px"});
    }
    lastScrollTop = scope.scroll;
  }
  windowEl.on('scroll', scope.$apply.bind(scope, handler));
  handler();
}
}


}
  };
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28839433

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档