亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 Angular JS 上預填充關鍵字搜索過濾器

在 Angular JS 上預填充關鍵字搜索過濾器

手掌心 2021-11-12 16:32:36
我正在嘗試在輸入字段中加載帶有預填充值的頁面。頁面和值確實會加載,盡管在我在鍵盤上輸入內容之前它不會觸發過濾結果中的任何內容。有沒有解決的辦法?我希望它在頁面加載后只加載過濾后的結果。我是 Angular JS 的新手,但感謝任何形式的幫助或朝著正確的方向努力。我試過了:ng-init="search.keywords='initial'" 在輸入標簽上,這似乎不會導致任何過濾發生。$scope.search = { keywords: 'initial' }; 還會加載初始值,但不會觸發任何過濾。<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;            });        };任何幫助將不勝感激!
查看完整描述

2 回答

?
縹緲止盈

TA貢獻2041條經驗 獲得超4個贊

$watch 函數用于檢測輸入字段在加載到 DOM 后的任何變化。


所以,第一次使用它,你可以這樣做:


在元素上使用 ng-init 以在 DOM 加載時觸發過濾器方法。


ng-init="doFilter(search)"

或者


在實際監視開始之前,在控制器級別本身調用過濾器函數一次。


$scope.search = { keywords: 'initial' }; 

$svope.doFilter($scope.search);


查看完整回答
反對 回復 2021-11-12
?
慕斯王

TA貢獻1864條經驗 獲得超2個贊

您可以在 ng-init 指令中指定應該在頁面初始化時執行的方法:

ng-init="doFilter({keywords: 'initial'})"


查看完整回答
反對 回復 2021-11-12
  • 2 回答
  • 0 關注
  • 125 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號