gpt4 book ai didi

javascript - Ionic 保持指令

转载 作者:行者123 更新时间:2023-12-03 07:56:12 28 4
gpt4 key购买 nike

我有一个带有集合重复的 div,我在其上应用了 ng-class ng-class="$index%2 == '0' ? 'blue' : 'yellow'",I还应用 ionic 保留指令作为ng-class="{'selected': column.selected}"?我希望当选择该行时,应应用所选类的红色。问题是,如果我先编写第二个 ng 类,则应用红色,但第一个 ng 类变得无效。有没有办法同时实现这两个目标

<div class="row brd rowclass" collection-repeat="column in tasklist_records track by $index" on-hold="onHoldFlag == true?'':onHold(column,$index)" ng-class="{'selected': column.selected}" ng-class="{white:$index%2 == 0,grey:$index%2 == 1,firstrow:$first}" item-height="50px" ng-click="goToAccountDetailScreen(column,$index)">

最佳答案

ng-class 中,您可以插入任何 Angular 表达式:为什么不使用函数(在范围上定义)来分析您想要的条件并返回包含以下内容的字符串正确的类名(空格分隔)?

这是一个例子:

angular.module('ionicApp', ['ionic'])

.controller('MyCtrl', function($scope) {
console.log("MyCtrl");
$scope.tasklist_records = [{
id: 0,
onHoldFlag: false
}, {
id: 1,
onHoldFlag: false
}, {
id: 2,
onHoldFlag: false,
selected: true
}, {
id: 3,
onHoldFlag: false
}, {
id: 4,
onHoldFlag: false
}, {
id: 5,
onHoldFlag: false
}, {
id: 6,
onHoldFlag: false
}];

$scope.customClass = function(column, $index) {
if (!column) return;
var _class = "";
if (column.id==0) _class += "firstrow ";
if ($index % 2 == 0) _class += "grey ";
if ($index % 2 == 1) _class += "white ";
if (column.selected) _class += "selected ";
console.log("customClass", column,_class);
return _class;
}

$scope.onHold = function(column, $index) {
console.log("onHold", column, $index);
column.onHoldFlag = !column.onHoldFlag;
}

$scope.goToAccountDetailScreen = function(column, $index) {
console.log("goToAccountDetailScreen", column, $index);
}

});
.selected {
color: red;
}
.white {
background-color:white;
}
.grey {
background-color:grey;
}
.firstrow {
color: blue;
}
<html ng-app="ionicApp">

<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<title>Ionic List can swipe</title>
<link href="//code.ionicframework.com/nightly/css/ionic.css" rel="stylesheet">
<script src="//code.ionicframework.com/nightly/js/ionic.bundle.js"></script>
</head>

<body ng-controller="MyCtrl">
<ion-header-bar class="bar-positive">
<h1 class="title">Ionic List</h1>
</ion-header-bar>

<ion-content>
<ion-list>
<div class="row brd rowclass" collection-repeat="column in tasklist_records track by $index" on-hold="onHoldFlag == true?'':onHold(column,$index)" ng-class="customClass(column, $index)" item-height="50px" ng-click="goToAccountDetailScreen(column,$index)">{{column.id}} - {{column.onHoldFlag}} - {{column.selected}}
</div>
</ion-list>
</ion-content>

</body>

</html>

关于javascript - Ionic 保持指令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34788851/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com