Jquery prevAll() in Angular way
Jquery prevAll() in Angular way
我有4个空的星形图标。每次它向前移动,前一颗恒星都会被遮蔽。就像这个链接中Jquery
示例中的prevAll()
一样。但我希望它能以Angular的方式完成。
到目前为止,这是我的工作:
<ul>
<li test-case condition="someConditions" class="star glyphicon glyphicon-star-empty"></li>
<li test-case condition="someConditions" class="star glyphicon glyphicon-star-empty"></li>
<li test-case condition="someConditions" class="star glyphicon glyphicon-star-empty"></li>
<li test-case condition="someConditions" class="star glyphicon glyphicon-star-empty"></li>
</ul>
我的指令:
.directive('testCase', function () {
return {
restrict: 'A',
scope: {
'condition': '='
},
link: function (scope, element, attrs) {
scope.$watch('condition', function(condition){
if(condition){
element.prevAll().addClass('glyphicon-star'); // this line won't work
}
})
}
}
});
我没有在li
中包含所有条件。所以,别介意我怎么才能知道恒星的进展在哪里。
知道怎么做吗?
.prevAll()
方法不是由jqLite提供的。为此,您需要使用jQuery或更好地使用ng-class
指令。
更多关于角度元件的信息
带有ng-class
指令:
<li test-case ng-class="{glyphicon-star:someConditions}"></li>
Plnkr在行动。
如果您不想加载完整的jQuery库(因为jqLite没有提供prevAll),那么prevAll的Vanilla JavaScript替代品是以下代码:
var prevAll = true;
prevAll = [].filter.call(<htmlElement>.parentNode.children, function (htmlElement) {
return (htmlElement === <htmlElement>) ? prevAll = false : prevAll;
});
prevAll将在一个数组中包含<htmlElement>
中所有以前的HTMLElement。
因此,在您的情况下,下面的代码可以完成任务:
.directive('testCase', function () {
return {
restrict: 'A',
scope: {
'condition': '='
},
link: function (scope, element, attrs) {
scope.$watch('condition', function(condition){
var element = element[0],
prevAll = true;
if(condition){
prevAll = [].filter.call(element.parentNode.children, function (htmlElement) {
return (htmlElement === element) ? prevAll = false : prevAll;
});
$(prevAll).addClass("glyphicon-star");
// or alternativly
/*
* [].forEach.call(prevAll, function (item) {
* item.classList.add("glyphicon-star");
* });
*/
}
})
}
}
});
更多适用于jqLite和jQuery的Vanilla Alternave
您可以以不同的方式思考,并在没有jQuery的情况下尝试:
你认为如何制定一个指令,由谁接收恒星的数量,并为你渲染每一颗恒星?
var app = angular.module('example', []);
app.controller('exampleController', function($scope) {
$scope.fool = {
rating: 5
}
});
app.directive('testCaseCollection', function () {
var getStars = function(rating) {
var stars = [];
for (var i = 1; i <= 5; i++) {
stars.push({
active: (i <= rating)
});
}
return stars;
};
return {
restrict: 'A',
scope: {
'rating': '='
},
link: function (scope, element, attrs) {
scope.$watch('rating', function(rating) {
scope.stars = getStars(rating);
})
},
template: '<ul><li ng-repeat="star in stars" ng-class="star.active ? ''cls-1'' : ''cls-2''">{{star.active}}</li></ul>'
}
});
.cls-1 {
color: red;
}
.cls-2 {
color: green;
}
<!DOCTYPE html>
<html ng-app="example">
<head>
<link rel="stylesheet" href="style.css" />
<script src="https://code.angularjs.org/1.4.8/angular.js"></script>
<script src="app.js"></script>
</head>
<body ng-controller="exampleController">
<input type="range" min="0" max="5" ng-model="fool.rating" />
<p>Your selected rating is: {{fool.rating}}</p>
<div test-case-collection data-rating="fool.rating"></div>
</body>
</html>
相关文章:
- Angular JS IE9 Hashbang url rewriting
- 如何使用skip参数使用angular ui引导进行服务器端分页
- 在自定义mean.io包中使用angular-chart.js作为依赖项
- 无法在数据endVal中设置值=“”;{{ucount}}”;使用Angular JS的CountUp
- 使用angular重定向到html页面
- angular.js没有'无法在PhoneGap中处理视图标记
- Javascript(Angular)从一个对象数组到第二个数组查找值
- angular 1.5应用程序中的导航栏
- angular的下拉菜单
- Angular只从数组中获取所需的数据
- 如何将不可变的js导入angular 2(alpha)
- Angular js-返回一个包含类似
- 如何使用 Angular JS 将数据保存在数据库中
- 将JSON对象传递给angular指令
- 从HTTPGET返回一个自定义对象列表,以便在Angular 2应用程序中使用
- Angular:更新一次性绑定的数据
- Jquery prevAll() in Angular way
- 2-way数据绑定在Angular UI的select标签中不起作用
- 使用流星和Angular为单个文档提供3- way数据绑定,而不是集合
- Angular Datatables ng-click 不给出绑定(Angular way)