我正在嘗試在輸入字段中加載帶有預(yù)填充值的頁面。頁面和值確實會加載,盡管在我在鍵盤上輸入內(nèi)容之前它不會觸發(fā)過濾結(jié)果中的任何內(nèi)容。有沒有解決的辦法?我希望它在頁面加載后只加載過濾后的結(jié)果。我是 Angular JS 的新手,但感謝任何形式的幫助或朝著正確的方向努力。我試過了:ng-init="search.keywords='initial'" 在輸入標(biāo)簽上,這似乎不會導(dǎo)致任何過濾發(fā)生。$scope.search = { keywords: 'initial' }; 還會加載初始值,但不會觸發(fā)任何過濾。<input type="text" id="search-keywords" ng-model="search.keywords" 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;};$scope.filterByKeywords = function (courses, keywords) { if (!keywords || keywords == "") { return courses.filter(function (course) { return true; }); } var keywordArr = keywords.toLowerCase().trim().replace(/\W+/g, " ").replace(/\s\s+/g, " ").split(","); return courses.filter(function (course) { var matched = false; for (var i = 0, length = keywordArr.length; i < length; i++) { if (course.keywords && course.keywords.indexOf(keywordArr[i]) > -1) { matched = true; break; } } return matched; }); };任何幫助將不勝感激!
在 Angular JS 上預(yù)填充關(guān)鍵字搜索過濾器
手掌心
2021-11-12 16:32:36