javascript - 在我的例子中,如何获取 Select 元素中的选定值?

标签 javascript html angularjs

当用户以 Angular 方式选择选项时,我试图获取选项值。我有类似的东西'

<div ng-controller="test">
  <select ng-model="selectedProduct" ng-options="product.name for product in products">
     <option value="">Please select a product</option>
  </select>
</div>

在我的js中

app.controller('test', function ($scope, $http, $modal) {

    console($scope.selectedProduct)   -> undefined. 

})

我想在用户做出选择时获取所选值。谁能帮我解决这个问题吗?非常感谢!

最佳答案

只要 View 中的值发生变化,您就可以使用 ngChange 触发事件:

<select ng-model="selectedProduct" ng-options="product.name for product in products"
        ng-change="doSomething(selectedProduct)">
    <option value="">Please select a product</option>
</select>

app.controller('test', function ($scope, $http, $modal) {
    ...
    $scope.doSomething = function (product) {
        // Do stuff with product
    };
<小时/>

另请参阅此 short demo

关于javascript - 在我的例子中,如何获取 Select 元素中的选定值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24516305/

相关文章:

javascript - DataTables 无法添加行

javascript - Base64 图像源无法在 Cordova IOS 上运行

html - 无法在移动 View 上设置第一个 flex tile

html - css:为什么文本会在删除位置后向上移动:绝对

angularjs - 如何从 $http 请求中获取数据以显示在 Angular ui.grid 中

javascript - 拖动时出现意外的边距/填充 (jQuery Ui)

javascript - Angular2,doe 的 EventEmitter 需要 zone.run?

javascript - 像时间序列一样在条形图中继续显示状态数据的 Highcharts

javascript - jQuery DatePicker 无法在 AngularJs 模式中工作

angularjs - generator-angular - 您似乎没有安装名称为 angular 的生成器