我正在智能表组件中显示一些数据(使用
angular
)。最后一列是一个选择元素。我想要做的是插入“on-select
”更改事件并对行中显示的数据执行某些操作。
这是我的笨蛋:
我想要达到的目标都在评论里展示了
如果你想创建一个下拉列表,最好使用 ng-options 而不是 ng-repeat。
<select ng-options="x.data for x in myItems">
</select>
因为 ngRepeat 指令为数组中的每个项目重复一段 HTML 代码,所以它可用于在下拉列表中创建选项,但 ngOptions 指令专门用于用选项填充下拉列表,并且至少有一个重要优势:
使用 ng-options 制作的下拉菜单允许所选值是一个对象,而使用 ng-repeat 制作的下拉菜单必须是字符串。
添加 ng-change 以及所需的 ng-model,如果需要访问其值,则将 ng-model 变量传递到 ng-change 中:
<select ng-change="handleChange(selectedRow)" ng-model="selectedRow" ng-options="x.data for x in myItems">
</select>
然后在我们的控制器中:
$scope.handleChange = function(row){
//here take action or do whatever you want to. if you want to access the value, you do it through 'row' here, e.g. console.log(row)
}