javascript - 如何使用 AngularJS 获取数组中的元素

标签 javascript arrays angularjs

我正在使用 AngularJS,并且我得到了一个带有静态选项的简单选择元素。当我单击一个选项时,我想获取数组的某个索引上的元素。
如果这很荒谬,请原谅我,但我是 Angular JS 的新手

这是类和下拉列表

 $scope.products=[
            {
                "Id":1,
                "Name":"ice cream",
                "Price":8.50
            },
            {
                "Id":2,
                "Name":"salad",
                "Price":8.99
            },
            {
                "Id":3,
                "Name":"fish & chips",
                "Price":10.91
            }
        ]
<select name="singleSelect" ng-model="data.selectedOption">
            <option ng-repeat="product in products" ng-model="selectedOption" value="{{product.Id}}">{{product.Name}}</option>
</select>

例如,当我点击冰淇淋时,下拉列表旁边应该有该商品的价格。

最佳答案

select上使用ngOptions - 在select上放置一个ngModel - 然后简单地显示!

<select name="singleSelect" ng-model="data.selectedOption" ng-options="product as product.Name for product in products">
</select>

<span>Price: {{data.selectedOption.Price}}</span>

关于javascript - 如何使用 AngularJS 获取数组中的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34885652/

相关文章:

javascript - Angularjs-如何在 ng-repeat 中重复 tr

javascript - 如何在 angularjs 中使用 javascript 代码?

javascript - 如何在 Angular js中将数据从工厂传递到 Controller ?

javascript - 如何通过 window.open 传递数据或对象

javascript - 应用自定义样式时拆分 HTML 标签

c++ - 字符数组在 C++ 中不起作用

C 可以推测我想用哪个数组来存储我的字符吗?

javascript - 使用 jquery 日历时无法检测到输入

javascript - 将按钮作为 Material-ui 表中的 TableRowColumn

c - 在结构中初始化 typedef 数组