简体   繁体   中英

Ember template helper get-value-with-key

Ember 是否有任何模板助手“get-value-with-key”我发现了以下用法,但不确定它到底是做什么的?

{{get-value-with-key item optionValuePath}}

There is an Ember Get Helper for HTMLBars. You might have to install the Package "ember-get-helper" if you are on ember < 2.1.

{{get object key}}

Let's say you have the following object:

var obj = {
  "key1": {
    "subkey1": "hello world"
  }
}

Using Ember 3.18, to access "hello world" from the template, you can do:

{{get obj 'key1.subkey1'}}

You can use the build-in get helper. See docs here: Ember Docs .

Usage example:

{{get object key}}

Note that the get helper will not be able to work on all JavaScript keys. For example, a key with a '.'will not work with the built-in get helper.

For example, if you have a valid JavaScript object like:

const example = {
    'example.pdf': 'pdf_url'
}

// You can access this key normally via
example['example.pdf']

however, this will not work in the get helper

{{get this.example 'example.pdf'}}

One solution is to create a helper that can support the types of keys you need to support. For example, I made a helper that can work on keys with a '.' by including '.' in the key name which are escaped like with ''.

{{get this.example 'example\.pdf'}}

The ember twiddle can be found here: twiddle

Other Helpful Sources:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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