我想要的是類似this文檔中的示例,但具有可以發揮「any」,「name」或「phone」屬性的過濾功能的唯一輸入,角色由錨點擊完成。 以下是準備好的代碼http://jsfiddle.net/ubugnu/QuyCU/如何動態更新ng-model屬性?angularjs:動態更改過濾器選項
HTML
<div ng-app>
<div ng-controller="MainCtrl">
<label>Any</label> <input type="text" ng-model="search.$"> <br>
<label>Name only</label> <input type="text" ng-model="search.name"><br>
<label>Phone only</label> <input type="text" ng-model="search.phone"><br>
<div style="background-color:#FAE8F1">
<hr>
<label>Filter</label> <input type="text" ng-model="search.$"> by {{filter}} <br>
<ul>
<li><a href="" ng-click="changeFilterTo('$')">Any</a></li>
<li><a href="" ng-click="changeFilterTo('name')">By Name</a></li>
<li><a href="" ng-click="changeFilterTo('phone')">By phone</a></li>
</ul>
<hr>
</div>
<table class="table">
<tr><th>Name</th><th>Phone</th></tr>
<tr ng-repeat="friend in friends | filter:search">
<td>{{friend.name}}</td>
<td>{{friend.phone}}</td>
</tr>
</table>
</div>
</div>
JS
function MainCtrl($scope, $http) {
$scope.friends = [{name:'John', phone:'555-1276'},
{name:'Mary', phone:'800-BIG-MARY'},
{name:'Mike', phone:'555-4321'},
{name:'Adam', phone:'555-5678'},
{name:'Julie', phone:'555-8765'}];
$scope.filter = "$";
$scope.changeFilterTo = function(pr) {
$scope.filter = pr;
}
};
優秀,非常感謝 – ubugnu
這並不完全工作,在小提琴...嘗試尋找另一種簡單的方法'a'並點擊'電話'。無論點擊的鏈接如何,它似乎都在搜索「全部」。不過,我認爲它只需要一小部分的調整即可。 –
我認爲它工作正常,但也許以一種非直觀的方式(但它很好地演示瞭解決方案) - 當您單擊「任何|按名稱|通過電話」時,它只是將底部文本框鏈接到相應的上部文本框之一。因此,輸入'a'填充到'Any'並點擊'通過電話'將底部文本框切換到「電話」,所以下一次輸入添加電話限制,只留下「任何」限制。 – lopisan