如何在鼠标悬停时更新repeat中元素的样式
How to update the style of an element inside a repeat on mouseover
我正试图在ng鼠标悬停器上更新ng repeat中元素的ng样式。我试过几个主意,但似乎都想不通。以下是我的代码背后的基本思想:
视图:
<div ng-repeat="data in days | orderBy:'timestamp'"
ng-style="seriesStyle(data)"
ng-mouseover="examineSeries(data)"></div>
控制器:
$scope.examineSeries = function (data) {
$scope.frame = data; // used for other things
// want to update the width of the mouseover'd element's seriesStyle
// here to something higher than 1%
}
$scope.seriesStyle = function (data) {
return {
'background-color': data.color,
'width': '1%'
}
}
请注意,由于其他依赖关系,我不能只在每个元素上放一个类并使用:hover
我认为处理这件事的正确方法是创建一个指令,该指令将检查鼠标悬停时的数据,并在需要时更新样式。看看这个jsfiddle,它展示了如何做到
HTML:
<div ng-app="myapp">
<div ng-controller="AppCtrl as ctrl">
<div ng-repeat="day in ctrl.days" ng-style="ctrl.seriesStyle(day)" update-on-mouse-over>{{day}}</div>
</div>
</div>
Javascript:
var myapp = angular.module('myapp', []);
myapp.controller('AppCtrl', function () {
this.days = [
{name:'Sun', color:'red'},
{name:'Mon', color:'blue'},
{name:'Tue', color:'orange'}
];
this.seriesStyle = function (day) {
return {
'background-color': day.color
};
};
});
myapp.directive('updateOnMouseOver', function() {
return {
link: function(scope, element, attrs) {
element.on('mouseover', function() {
element.css('background-color', 'green');
});
}
};
});
相关文章:
- 使用 ng-repeat访问 ng 表单元素/值
- AngularJS:根据其他对象预先选择ng repeat中的select元素
- Angular js将只显示ng repeat中的第一个元素
- 带有 ng-repeat 的 Angular JS 指令无法遍历子元素
- $compile和ng repeat未返回正确的元素
- 使用ng repeat时元素ID重复
- 验证嵌套在ng中的输入元素repeat也用于页面加载,而不仅仅用于更改
- AngularJS:如何使用ng repeat来显示父数组中包含的数组的所有元素
- 如何使用ng repeat在匹配的元素上添加活动类
- 使用ng repeat将表单元素绑定到角度中的新对象
- 如何只更新ng repeat中的部分元素
- 操作NG-REPEAT的第一个元素
- 将 'ng-repeat' 元素传递给 javascript 函数
- 以编程方式在 ng-repeat元素上切换类
- 使用AngularJS指令来更改ng-repeat元素的类
- 如何强制jQuery“监听”?为将来的AngularJS ng-repeat元素激活插件
- Ng-click适用于所有ng-repeat元素
- ng-repeat元素# 39;索引在排序时改变
- 将d3 svg附加到当前的ng-repeat元素
- 我可以在javascript中声明ng-repeat元素吗?