我正在尝试在输入字段中加载具有ng-init值的页面。现在页面已加载,尽管直到我在键盘上输入内容后,它才会触发筛选结果中的任何内容。有没有解决的办法?我希望它可以在页面加载后立即加载过滤后的结果。
我的JS经验相对较少,但是希望获得任何帮助或朝着正确的方向努力。请注意,还有大量的缺失-如有必要,我很乐意粘贴更多。
HTML
<input type="text" id="search-keywords" ng-model="search.keywords" ng-init="search.keywords='initial'" class="form-control" placeholder="Keyword search">
脚本
$scope.$watch("search", function (newVal, oldVal) {
if (newVal) {
$scope.doFilter(newVal);
}
}, true);
$scope.doFilter = function (search) {
$scope.filtering = true;
$scope.filteredCourses = $scope.filterExactMatchExceptNull($scope.courses, "inst", search.inst);
$scope.filteredCourses = $scope.filterExactMatchExceptNull($scope.filteredCourses, "fos", search.fos);
$scope.filteredCourses = $scope.filterCutoff($scope.filteredCourses, search.min, search.max);
$scope.filteredCourses = $filter("filter")($scope.filteredCourses, {
code: search.code,
name: search.name,
poa: search.poa
});
$scope.filteredCourses = $scope.filterByKeywords($scope.filteredCourses, search.keywords);
$scope.limit = 15;
if ($scope.limit >= $scope.filteredCourses.length) {
$scope.limit = $scope.filteredCourses.length;
}
$scope.filtering = false;
};
我希望过滤器处理最初加载的内容,但没有任何反应。
非常感谢您的时间和帮助!
ng-init="search.keywords='initial'"
您需要在ng-init上调用doFilter
并在函数内使用$ scope变量来确定值是什么,而不是将其传入。
第二:当您创建多个基于ui的数据点时,Angular 1会有一些怪癖。尝试将所有ui数据点包含到$ scope一级的对象中。所以像这样:
$scope.ui = {}; $scope.ui.filtering $scope.ui.filteredCourses $scope.ui.limit