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?