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.
Considering you don’t have a service that will return only the JSON formatted items, the best approach would be creating a directive that remove the
li
, parse their contents to an object and useng-repeat
in a template. Something like this:And in your HTML you just decorate the list with the directive:
I’ve put together a Plnkr here. Go ahead and change the HTML list and it will automatically include that items.
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
index.html
Edit I updated the plunker with a more complete example than the code shown here.
If you can have the JSON approach, then Angular automatically does that for you.
Just go with a simple filter solution:
In your controller (or any JS with access to the container scope):
You can even use
$http
service to retrieve the JSON file: