简体   繁体   English

Aurelia / Typescript-如何通过选择标签将对象绑定到变​​量

[英]Aurelia/Typescript - How do I bind an object to a variable thru select tag

So, I have a static array of objects in JSON. 因此,我在JSON中有一个静态对象数组。 I've populated a select tag with those options successfully and set the value of each option of the select tag to the object id. 我已经使用这些选项成功填充了select标签,并将select标签的每个选项的值设置为对象ID。

tvs: any[] = [
        { "id": 1, "ip": "11.11.11.111", "port": "8080", "name": "tv 1" },
        { "id": 2, "ip": "11.11.11.111", "port": "8080", "name": "tv 2" },
        { "id": 3, "ip": "11.11.11.111", "port": "8080", "name": "tv 3" },
        { "id": 4, "ip": "11.11.11.111", "port": "8080", "name": "tv 4" },
        { "id": 5, "ip": "11.11.11.111", "port": "8080", "name": "tv 5" },
        { "id": 6, "ip": "11.11.11.111", "port": "8080", "name": "tv 6" }
    ];

and then in my html file: 然后在我的html文件中:

<div class="col-md-6 col-lg-6">
     <label>Which tv do you want to alter?</label>
     <select class="form-control">
          <option repeat.for="tv of tvs" value="${tv.id}">${tv.name}</option>
     </select>
</div>

That works great, however I want to add a dynamic div below this one and have it change and show information based on the selected object. 效果很好,但是我想在该分区下添加一个动态div并对其进行更改并根据所选对象显示信息。 For example, if the user selects tv 5 in the select tag, I want it to show the information for tv 5. How do I bind the selected object to something so that I can accomplish this? 例如,如果用户在select标签中选择了tv 5,我希望它显示有关tv 5的信息。如何将所选对象绑定到某物,以便可以完成此操作?

Aurelia provides model.bind for use on option elements inside a select . Aurelia提供了model.bind用于select内的option元素。 For your example something like this should work: 对于您的示例,这样的事情应该起作用:

<div class="col-md-6 col-lg-6">
   <label>Which tv do you want to alter?</label>
   <select class="form-control" value.bind="selectedTV">
      <option repeat.for="tv of tvs" model.bind="tv">${tv.name}</option>
   </select>
</div>
<div if.bind="selectedTV">
  Info for TV ${selectedTV.name}:<br>
  ip: ${selectedTV.ip}<br>
  etc...
</div>

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

相关问题 我如何获取HTML标记内的值并将其绑定到aurelia中的ts(组件文件) - How can i get the value which is inside the HTML tag and bind it to the ts(component file) in aurelia 如何将TypeScript泛型与绑定一起使用? - How do I use TypeScript generics with bind? 如何从Typescript / Angular 7中的对象变量获取类型? - How do I get the type from an object variable in Typescript / Angular 7? 如何在打字稿Angular2中将JSON对象与变量绑定? - How to bind json object with variable in typescript Angular2? 我想创建仅允许数字的文本框。 如何使用aurelia中的Typescript做到这一点? - I want to Create textbox which allows only numbers. How can I do that with Typescript in aurelia? 我想在文本框中自动添加破折号 (-)。 如何在 aurelia typescript 应用程序中执行此操作? - I want to automatically add dashes (-) in a textbox. How can I do this in an aurelia typescript application? 如何在Aurelia打字稿应用程序中使用mobile-detect.js? - How do I use mobile-detect.js in an Aurelia typescript app? 如何在Aurelia中写入全局变量? - How can I write to a global variable in Aurelia? 如何使用打字稿为标签选择设置“大小”并做出反应, - how can i set "size" for the tag select using typescript and react, 如何在打字稿中访问此变量 - How do I access this variable in typescript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM