gpt4 book ai didi

javascript - Aurelia css 与函数绑定(bind)

转载 作者:行者123 更新时间:2023-12-02 13:56:44 25 4
gpt4 key购买 nike

允许使用 function 的正确方法是什么?而不是像这一行 <div class.bind="isSuccess ? 'success' : 'error'">${message}</div> 那样在类绑定(bind)中添加代码在app.html 中?这种示例在淘汰赛中使用时将分别更新类绑定(bind),即使可观察对象位于函数内部。

这是一个示例:https://gist.run?id=d2b120bcd3d6a8157f4d4c9bf247988b

app.html

<template>
<div class.bind="getColor()">${message}</div>
<div class.bind="isSuccess ? 'success' : 'error'">${message}</div>

<button click.delegate="toggleColor()">toggle color</button>
</template>

app.js

export class App {
message = 'hello worlds';
isSuccess = false;
toggleColor() {
this.isSuccess = !this.isSuccess;
}

getColor() {
return this.isSuccess ? 'success' : 'error';
}
}

index.html

<!doctype html>
<html>
<head>
<title>Aurelia</title>
<link rel="stylesheet" href="https://gist.host/run/1479356763275/style.css">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body aurelia-app>
<h1>Loading...</h1>

<script src="https://cdn.rawgit.com/jdanyow/aurelia-bundle/v1.0.3/jspm_packages/system.js"></script>
<script src="https://cdn.rawgit.com/jdanyow/aurelia-bundle/v1.0.3/config.js"></script>
<script>
System.import('aurelia-bootstrapper');
</script>
</body>
</html>

样式.css

/* Styles go here */

.success {
background-color: green;
}

.error {
background-color: red;
}

最佳答案

绑定(bind)到计算属性时应该使用 getter 函数。例如:

JS

import {computedFrom} from 'aurelia-framework';

export class App {
message = 'hello worlds';
isSuccess = false;
toggleColor() {
this.isSuccess = !this.isSuccess;
}

@computedFrom('isSuccess') //use @computedFrom to avoid dirty-checking
get getColor() {
return this.isSuccess ? 'success' : 'error';
}
}

HTML

<div class.bind="getColor">${message}</div>

关于javascript - Aurelia css 与函数绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40646809/

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