I’ve just started using angularjs and I’m trying to connect a wordpress back-end to an Angularjs front end.
this is my index.html
<!DOCTYPE html>
<html ng-app="applicazioneIndex">
<head>
<title>AngularJS GET request with PHP</title>
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.24/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.24/angular-sanitize.min.js"></script>
<link data-require="bootstrap-css@2.3.2" data-semver="2.3.2" rel="stylesheet" href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css" />
<script src="//cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/0.11.0/ui-bootstrap-tpls.min.js"></script>
<script data-require="angular-resource@*" data-semver="1.2.14" src="http://code.angularjs.org/1.2.14/angular-resource.js"></script>
<link rel="stylesheet" href="style.css" />
<script src="script.js"></script>
<meta charset="UTF-8">
</head>
<body >
<div class="row">
<div class="container">
<div ng-controller="ApplicazioneController">
<input type="text" ng-model="searchFilter" class="form-control">
<table class="table table-hover">
<thead>
<tr>
<th>{{currentPage}} di {{totalItems}} {{watchPage}}</th>
<th>post_content</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="post in filteredArticoli | filter:searchFilter">
<td>{{post.title}}</td>
<td><a ng-href="{{post.featured_image.source}}"><img ng-src="{{post.featured_image.attachment_meta.sizes.thumbnail.url}}"></a></td>
<td ng-bind-html="post.excerpt"></td>
</tr>
</tbody>
</table>
<pagination boundary-links="true"
max-size="5"
items-per-page="itemsPerPage"
total-items="totalItems"
ng-model="currentPage"
ng-change="pageChanged(page)"></pagination>
</div>
</div>
</div>
</body>
</html>
and this is my script.js with angular code (I know it’s all in one file and it’s bad)
var req = new XMLHttpRequest();
var URL = "/wp-json/posts";
req.open('GET', URL, false);
req.send(null);
var maxpagine = req.getResponseHeader("X-WP-TotalPages");
var totarticoli = req.getResponseHeader("X-WP-Total");
var currentPage = '';
var newPage = '';
var app = angular.module('applicazioneIndex', ['ui.bootstrap','ngSanitize','ngResource']);
app.controller('ApplicazioneController', function ($scope, applicazioneFactory) {
applicazioneFactory.get({ id: 1 }, function(data) {
$scope.posts = data;
alert(posts);
});
$scope.articoli = applicazioneFactory.query();
$scope.itemsPerPage = 3;
$scope.currentPage = 1;
$scope.totalItems = totarticoli;
$scope.articoli.$promise.then(function () {
$scope.$watch('currentPage + itemsPerPage', function() {
var begin = (($scope.currentPage - 1) * $scope.itemsPerPage),
end = begin + $scope.itemsPerPage;
$scope.filteredArticoli = $scope.articoli.slice(begin, end);
});
});
$scope.$watch('currentPage', function(newPage){
$scope.watchPage = newPage;
alert(newPage);
});
});
app.factory('applicazioneFactory', function ($resource) {
return $resource( "/wp-json/posts?page=" + currentPage);
});
Pagination is working perfectly, I get the values from a XMLHttpRequest, the bootstrap menu is populated, but I can’t populate content with different JSON.
My goal is to load different JSON files using $currentPage
value, changing while bootstrap pagination is working (for that purpose there is an alert to pop up currentPage value).
using $scope.$watch('currentPage', function(newPage)
I can get currentPage value inside the controller
I’ve also a Plunker here http://plnkr.co/edit/hplfuBIOLDHUgUp6lU0A where I’ve put a static JSON file instead of the dynamic RESTful request url.
How can I put the $currentPage
value inside app.factory?
It will be easier when you load all of your posts and stores them into an
$scope
variable. And on this variable you can apply an filter. In AngularJS it is possible to create your custom filters.Lets have a look in module documentation
filter
.Example
Normally it should be possible to control the number of given posts via REST-interface.
UPDATE
Okay I believe now what you want. You can add your own resource actions. Something like this:
You can call this method like this:
HTTP-Requests:
http://<yourhost>/wp-json/posts/?count=5&page=1&post_type=Category1
http://<yourhost>/wp-json/posts/?count=5&page=1&post_type=Category2
http://<yourhost>/wp-json/posts/?count=5&page=1&post_type=Category3
More information you will find in the documentation: ngResource
UPDATE 2
You don’t need an variable inside your factory and it is no problem to use bootstrap pagination. Please show below I have an function
pageChange()
inside from your controllers$scope
.HTML-Snippet
Everytime you click on pagination the function
pageChange()
will be called and send the following HTTP-Requests:http://<yourhost>/wp-json/posts/?page=1
Hope this will help you
Thanks to this article:
How to load json into my angular.js ng-model?
I’ve found the solutions: Delete the useless app.factory, load all data in controller and rotate the JSON using currentPage change from bootstrap pagination.
This is the working script.js code:
Many thanks to Sven Schürmann that helped me in looking for different directions.