如何在选择NG-重复选择选项值后调用函数



我有一个选择标签:

<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

所以我在页面加载时选择它。但是分页不起作用

当我从下拉列表中change()查看其他部门数据时,结果如我预期的那样很好,包括分页..!

有没有办法调用change()函数来获取所选值的正确数据。? 而不是更改下拉列表。!当选定的触发器时,有没有其他方法可以调用change()函数。!

ng-init="department=data.usercurrentDeptID"  

如角度文档中所述,如果以编程方式更改模型而不是通过更改输入值,则不会评估 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();
}

最新更新