所有你需要做的是編寫一個函數,採取各種參數。最好的部分是,這不會觸發髒檢查。如果參數的值發生變化,Aurelia只會調用函數。
下面是一個例子:https://gist.run?id=837b35139b924682143e74f4e7ca85ba
app.html
<template>
<ul class="all-steps">
<li repeat.for="step of steps" class="${calculateClass(current, step)}">Step 1: ${$index + 1} (${calculateClass(current, step)})</li>
</ul>
</template>
app.js
export class App {
current = 3;
steps = [ 1, 2, 3, 4, 5 ];
previous = [ 1, 2 ];
calculateClass(current, step) {
console.log(`called. current = ${current}, step = ${step}`)
if(current === step) {
return 'active';
} else if (this.previous.indexOf(step) >= 0) {
return 'done';
} else {
return '';
}
}
}
的index.html
<!doctype html>
<html>
<head>
<title>Aurelia</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
li {
background-color: green;
}
li.active {
background-color: yellow;
}
li.done {
background-color: red;
}
</style>
</head>
<body aurelia-app>
<h1>Loading...</h1>
<script src="https://jdanyow.github.io/rjs-bundle/node_modules/requirejs/require.js"></script>
<script src="https://jdanyow.github.io/rjs-bundle/config.js"></script>
<script src="https://jdanyow.github.io/rjs-bundle/bundles/aurelia.js"></script>
<script src="https://jdanyow.github.io/rjs-bundle/bundles/babel.js"></script>
<script>
require(['aurelia-bootstrapper']);
</script>
</body>
</html>
非常感謝我唯一缺少的東西是什麼時候該函數被觸發:) –