简体   繁体   English

使用Aurelia有条件地显示Element

[英]Conditionally display Element using Aurelia

So I have my auth class injected into my main.js : 所以我将我的auth类注入到我的main.js

import {Auth} from 'auth';
import {inject} from 'aurelia-framework';

@inject(Auth)
export class App {
    constructor(auth) {
        this.auth = auth;
    }

    get isLoggedIn() { return this.auth.isLoggedIn; }
}

so in my app.html 所以在我的app.html

<form>
    <!-- form login elements -->
</form>

how do I make this element conditionally display based on my app getter function. 如何根据我的app getter函数有条件地显示此元素。

You can use if.bind to conditionally bind your DOM elements. 您可以使用if.bind有条件地绑定DOM元素。

 <form>
      <div if.bind="auth.isLoggedIn">
        <!--this DOM element will be bind only if auth.isLoggedIn is true-->
      </div>
 </form>

Optionally, you can also use show.bind but that will only hide your DOM elements. 或者,您也可以使用show.bind,但这只会隐藏您的DOM元素。 Where as if.bind will not render it on your page. if.bind将不会在您的页面上呈现它。

If you need to remove element completely from markup when condition is not met, you can use if.bind (as @Pratik Gajjar answered): 如果你需要在不满足条件时完全从标记中删除元素,你可以使用if.bind (如@Pratik Gajjar回答):

<template>
  <div if.bind="auth.isLoggedIn">
    <!--this DOM element will be bind only if auth.isLoggedIn is true-->
  </div>
</template>

If you need to conditionally set display: none on element, you can use show.bind : 如果需要在元素上有条件地设置display: none ,可以使用show.bind

<template>
  <div show.bind="auth.isLoggedIn">
    <!--this DOM element will be shown only if auth.isLoggedIn is true-->
  </div>
</template>

For details have a look at http://aurelia.io/hub.html#/doc/article/aurelia/framework/latest/cheat-sheet/6 . 有关详细信息,请查看http://aurelia.io/hub.html#/doc/article/aurelia/framework/latest/cheat-sheet/6

So I created a value converter: 所以我创建了一个值转换器:

export class CssdisplayValueConverter {
    toView(value) {
        return value ? 'none' : 'display';
    }
}

Then in my app.html: 然后在我的app.html中:

<require from='css-display'></require>

<form css="display: ${isLoggedIn() | cssdisplay}"></form>

Boom, done. 热潮,完成了。

您可以使用if.bindshow.bind通过检查条件来绑定元素

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM