angular.js - 關於angularjs中filter問題
滿天的星座
滿天的星座 2017-05-15 16:56:17
0
3
608

  • 一般都是這樣寫過濾,但是我想只針對一部分數據,phone.name實現過濾這個要如何實現呢? ?

  • 滿天的星座
    滿天的星座

    全部回覆(3)
    漂亮男人

    根據官方文件的描述,有幾種方式來實現:
    {{ filter_expression | filter : expression : comparator}}

    1.如Guox給出的方法,使用expression:

    Object: A pattern object can be used to filter specific properties on objects contained by array. For example {name:"M", phone:"1"} predicate will return an array of itemsms whichnty name" property phone containing "1".

    作為Object使用,用於過濾數組元素的指定屬性,如expression = {name:"M", phone:"1"} ,那麼將會過濾出數組中name包含'M'且phone包含'1'的元素。

    故使用以下方法即可

    <li ng-repeat="phone in phones|filter:{name:query}">
    

    2.可以使用comparator:

    Comparator which is used in determining if the expected value (from the filter expression) and actual value (from the object in the array) should be considered a match.
    function(actual, expect ): match.

    function(actual, expect. and the predicate value to compare and should return true if both values should be considered equal.

    透過Comparator的回傳值能夠決定是否匹配,入參分別是actual(陣列中的元素)和expected(輸入)

    那麼定義一個函數🎜
    $scope.customerFilter = customerFilter;
    
    function customerFilter(actual, expected){
        return actual.name.contains(expexted) ? true : false;
    }
    
    前端使用
    <li ng-repeat="phone in phones|filter:query:customerFilter">
    
    習慣沉默

    太感謝二位了,謝謝

    熱門教學
    更多>
    最新下載
    更多>
    網站特效
    網站源碼
    網站素材
    前端模板