douzhuan1432 2013-09-27 00:13
浏览 36
已采纳

使用angularJS过滤PHP列表

I am trying to create a blog page and I chose WordPress over AngularJS so Google can index the page ( or at least that's what i think it works). So for now I have a list which looks like this

<ul>
    <li id="1">
        <h2>My first Post</h2>
        <p>The Message...</p>
    </li>
    <li id="2">
        <h2>My second Post</h2>
        <p>The Message...</p>
    </li>
    <li id="3">
        <h2>My third Post</h2>
        <p>The Message...</p>
    </li>
</ul>

but PHP is pretty static so I want to create a angular filter to filter posts by title, but I don't really know how to do this.

I was thinking to create a hide class for <li> items and somehow if a post should be deleted because of the filter, to add the hide class to it. I try to mix this angular so I can have a dynamic search instad loading the page again after searching.

  • 写回答

3条回答 默认 最新

  • douli4852 2013-10-07 01:11
    关注

    You could create a directive to wrap the html content you receive from php, pass the filter term and which element of the list you want to check).

    Here is a plunker: http://plnkr.co/edit/Bv2opi5CHfJa0pQyFrBc?p=preview

    (this require jquery to hide and show, but you can use css({'display':'none|block'}) too)

    (maybe you could modify the directive to apply the filter term to ignore the case of the words)

    app.js

    var app = angular.module('plunker', []);
    
    app.controller('MainCtrl', function($scope) {
        $scope.model = {
            filter: ''
        };
    });
    
    app.directive('myHtmlFilter', [function() {
        return {
            restrict: 'A',
            scope: {
              filter: '=myHtmlFilter',
              element: '@'
            },
            link: function(scope, elem, attrs) {
              scope.$watch('filter', function(newval, oldval) {
                elem
                  .find('ul>li')
                      .hide()
                      .find(scope.element)
                      .filter(':contains("'+scope.filter+'")')
                   .parent()
                      .show();
              })
            }
        }
    }]);
    

    index.html

    <input type="text" ng-model="model.filter" />
    
    <div my-html-filter="model.filter" element="h2">
      <ul>
        <li id="1">
            <h2>My first Post</h2>
            <p>The Message...</p>
        </li>
        <li id="2">
            <h2>My second Post</h2>
            <p>The Message...</p>
        </li>
        <li id="3">
            <h2>My third Post</h2>
            <p>The Message...</p>
        </li>
      </ul>
    </div>
    

    Edit I updated the plunker with a more complete example than the code shown here.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?