定義的參數進行搜索所以我有一個搜索框,其中有一些參數,我想限制搜索範圍。這包括我的例子中的名字和姓氏。Angular JS:使用參數
所以我一直在努力實現這個像這樣。這是下拉列表的代碼。
<select id="usersearchby" ng-model="searchFilter" ng-change="selectAction()">
<option>Search By ...</option>
<option value="lastName">Last Name</option>
<option value="firstName">First Name</option>
</select>
這是輸入搜索字段的代碼。
<input type="search" ng-if="searchFilter != 'firstName' && searchFilter != 'lastName'" ng-model="searchText">
<input type="search" ng-if="searchFilter == 'firstName'" ngModel="searchText.first_name">
<input type="search" ng-if="searchFilter == 'lastName'" ngModel="searchText.last_name">
這是顯示結果的div的代碼。
<div class="block_line_item userlist_expanded" ng-repeat="user in users | filter: searchText">
此代碼不工作,但是,如果我只是有說FIRST_NAME(沒有條件輸入)單個輸入搜索框,它工作得很好!我究竟做錯了什麼?有沒有更好的方法來做到這一點?
JSON數據我工作的一個示例:
[{"role":"1","credentials":"","work_phone":"206–298–3059","last_name":"Doe","middle_name":"","suffix":"","title":"Mr.","mobile_phone":"1–877–932–4259","id":1,"fax":"135–678-6357","first_name":"John","email":"[email protected]","username":"user1"}, {"role":"1","credentials":"","work_phone":"206–298–3059","last_name":"Doe","middle_name":"","suffix":"","title":"Mr.","mobile_phone":"1–877–932–4259","id":2,"fax":"135–678–6357","first_name":"Jane","email":"[email protected]","username":"user2"}]
能否請您提供您的控制器代碼? – 2014-12-08 06:46:09
非常基本的:'$ http.get(「/ users」)。success(function(data){ $ scope.users = data; });'用戶數據在json中,包含first_name和last_name的鍵值對等等。 – geekchic 2014-12-08 06:48:30
它看起來像過濾器有問題:searchText,因爲現在值在searchText.first_name或searchText.last_name – 2014-12-08 06:51:02