如何在 ng-repeat select 选项值得到 selected 后调用函数
how to call function after ng-repeat select option value got selected
我有一个 select 标签:
<select
ng-model="department"
ng-options='option.id as option.name for option in data.deptCategories'
ng-init="department=data.usercurrentDeptID"
ng-change="change()" >
</select>
将有一个包含所有部门的对象deptCategories
。
和usercurrentDeptID
将有用户部门id
所以我在 select 加载页面时就这样做了。但是分页不起作用
当我 change()
从下拉列表中查看其他部门数据时,结果如我所料,包括分页,结果很好..!
有什么方法可以调用 change()
函数来获取 selected 值的正确数据。?
而不是改变下拉菜单。!当 selected 触发时,还有其他方法可以调用 change()
函数吗?!
ng-init="department=data.usercurrentDeptID"
如 angular documentation ng-change 中所述,如果以编程方式更改模型而不是通过更改输入值更改模型,则不会对其进行评估。您可以使用 $scope.$watch 来处理模型更改时的更改。
$scope.$watch("department", function (newValue, oldValue) {
//handle changes
$scope.change();
});
使用 $scope.$watch 你将不需要 ng-change 因为 $scope.$watch 将处理更改事件。
我认为最简单的解决方案是让您的 ng-init
调用一个函数,该函数将做两件事,将部门设置为当前正在做的,以及调用 change()
,因为这就是加载分页数据的原因。您将离开 ng-change
方法,因为这将处理对下拉列表的后续更改,但新的 ng-init
方法将处理原始负载。
.html
<select
ng-model="department"
ng-options='option.id as option.name for option in data.deptCategories'
ng-init="initDepartment(data.usercurrentDeptID)"
ng-change="change()" >
</select>
.js
// in your controller
$scope.initDepartment = function (currentDeptId) {
$scope.department = currentDeptId;
$scope.change();
}
我有一个 select 标签:
<select
ng-model="department"
ng-options='option.id as option.name for option in data.deptCategories'
ng-init="department=data.usercurrentDeptID"
ng-change="change()" >
</select>
将有一个包含所有部门的对象deptCategories
。
和usercurrentDeptID
将有用户部门id
所以我在 select 加载页面时就这样做了。但是分页不起作用
当我 change()
从下拉列表中查看其他部门数据时,结果如我所料,包括分页,结果很好..!
有什么方法可以调用 change()
函数来获取 selected 值的正确数据。?
而不是改变下拉菜单。!当 selected 触发时,还有其他方法可以调用 change()
函数吗?!
ng-init="department=data.usercurrentDeptID"
如 angular documentation ng-change 中所述,如果以编程方式更改模型而不是通过更改输入值更改模型,则不会对其进行评估。您可以使用 $scope.$watch 来处理模型更改时的更改。
$scope.$watch("department", function (newValue, oldValue) {
//handle changes
$scope.change();
});
使用 $scope.$watch 你将不需要 ng-change 因为 $scope.$watch 将处理更改事件。
我认为最简单的解决方案是让您的 ng-init
调用一个函数,该函数将做两件事,将部门设置为当前正在做的,以及调用 change()
,因为这就是加载分页数据的原因。您将离开 ng-change
方法,因为这将处理对下拉列表的后续更改,但新的 ng-init
方法将处理原始负载。
.html
<select
ng-model="department"
ng-options='option.id as option.name for option in data.deptCategories'
ng-init="initDepartment(data.usercurrentDeptID)"
ng-change="change()" >
</select>
.js
// in your controller
$scope.initDepartment = function (currentDeptId) {
$scope.department = currentDeptId;
$scope.change();
}