在Aurelia视图中使用文字JavaScript值 [英] Using literal JavaScript values in an Aurelia view

查看:46
本文介绍了在Aurelia视图中使用文字JavaScript值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过使用"if"模板控制器来隐藏我的组件.问题是,即使我在其中添加了错误,它仍然可见:

I'm trying to make my component hidable by using "if" Template Controller. The problem is that it's visible even if I put false in it:

<template if.bind="${false}">
zzzzzzzz
</template>

if.bind ="false",没有绑定的两种情况都会带来相同的结果.难道我做错了什么?如果没有,您能否指向aurelia代码调试,这可能有助于我获得线索?

if.bind="false" and both cases without bind bring same result. Am I doing something wrong? If not, could you please point to aurelia code debugging which may help me to get a clue?

最诚挚的问候,尤金.

推荐答案

插值

使用if.bind表示Aurelia尝试评估表达式.因此,您不需要使用插值语法. 仅在将数据注入到不是由Aurelia管理的属性中时才要使用插值.典型的用例是将文本注入到任意元素中.

Interpolation

Using if.bind signals Aurelia to try to evaluate the expression. Therefore, you don't need to use the interpolation syntax. You only want to use interpolation when you are injecting data into a property that isn't managed by Aurelia. The typical use case is injecting text into an arbitrary element.

<p>Welcome to my website, you are the ${visitorCount} visitor!</p>

您还可以在任意要插入数据的地方(包括html属性)使用插值法.

You may also use interpolation in other places where you would arbitrarily like to inject data, including html attributes.

<input placeholder="${currentRecordType}" />

插值是一种单向绑定.在此处了解更多信息: http://aurelia.io/docs.html#string-interpolation

Interpolation is a one-way binding. Read more here: http://aurelia.io/docs.html#string-interpolation

现在,绑定语法用于挂钩Aurelia中启用的行为.开箱即用,其中包括一些模板逻辑,一些属性行为以及一些其他自定义内容.

Now, the binding syntax is for hooking into behaviors enabled in Aurelia. Out of the box, this includes some templating logic, some attribute behaviors, and some other custom things.

每个属性旨在检查在哪里使用它以及如何驱动它.例如,以下绑定将是单向的,因为它不接受输入,并且仅作为单向变量绑定才有意义.

Each attribute is designed to inspect where it is being used and how it should be driven. For example, the following binding will be one-way, as it does not accept input and only makes sense as a one way variable binding.

<div if.bind="sectionEnabled"></div>

但是,以下绑定将是双向的,因为它接受输入,因此通常会以双向方式运行.

However, the following binding will be two-way, as it accepts input and therefore will usually behave in the two-way manner.

<input value.bind="firstName />

但是请注意,没有语法可以说这是一个变量".实际上,Aurelia假定您要传递变量.如果您不这样做,例如在第二种情况下,您将不需要Aurelia,而只需编写:

Notice, though, that there is no syntax to say "this is a variable." In fact, Aurelia assumes you want to pass in a variable. If you don't, for example in the second case, you wouldn't require Aurelia and you would simply write:

<input value="firstName" />

您可以在此处阅读有关绑定语法的更多信息: http://aurelia.io/docs.html#databinding

You can read more about the bind syntax here: http://aurelia.io/docs.html#databinding

在您的情况下,您尝试通过绑定文字值来采取错误的方法. Aurelia正在寻找名为${false}/false的变量,很可能找不到它.您将要在viewModel中创建一个变量来绑定并指示您的视图绑定到该变量:

In your case, you're taking the wrong approach by trying to bind a literal value. Aurelia is looking for a variable named ${false}/false and most likely not finding it. You will want to create a variable in your viewModel to bind and instruct your view to bind to it:

视图模型

class viewModel {
  ...
  constructor() {
    this.showTemplate = true;
  }
}

视图

<template>
  <div if.bind="showTemplate">
    ... 
  </div>
</template>

这篇关于在Aurelia视图中使用文字JavaScript值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆