{"id":1154,"date":"2015-08-05T07:08:52","date_gmt":"2015-08-05T07:08:52","guid":{"rendered":"http:\/\/www.honobono-life.info\/wpeng\/?p=1154"},"modified":"2015-08-05T07:08:52","modified_gmt":"2015-08-05T07:08:52","slug":"microposts-feed-pagination-with-angularjs","status":"publish","type":"post","link":"http:\/\/www.honobono-life.info\/wpeng\/microposts-feed-pagination-with-angularjs\/","title":{"rendered":"Ruby on Rails Tutorial microposts feed pagination with AngularJS"},"content":{"rendered":"<p><strong>1)AngularJS template view<\/strong><\/p>\r\n\r\n<p>$ vi app\/assets\/templates\/static_pages\/home.html.erb<\/p>\r\n\r\n<pre class=\"prettyprint\">\r\n&lt;div ng-controller=&quot;HomeCtrl&quot;&gt;\r\n  &lt;div ng-show=&quot;chkSignin().user.id &gt; 0&quot; class=&quot;row&quot;&gt;\r\n    &lt;div class=&quot;col-sm-4&quot;&gt;\r\n.......\r\n    &lt;\/div&gt;\r\n    &lt;div class=&quot;col-sm-8&quot;&gt;\r\n      &lt;h3&gt;Micropost Feed&lt;\/h3&gt;\r\n      &lt;ol class=&quot;microposts&quot;&gt;\r\n        &lt;li ng-repeat=&quot;mp in currentitems&quot;&gt;\r\n...........\r\n        &lt;\/li&gt;\r\n      &lt;\/ol&gt;\r\n      &lt;pagination total-items=&quot;totalitems&quot;\r\n                  ng-model=&quot;currentpage&quot;\r\n                  max-size=&quot;maxsize&quot;\r\n                  items-per-page=&quot;itemsperpage&quot;\r\n                  class=&quot;pagination-sm&quot;\r\n                  previous-text=&quot;Previous&quot;\r\n                  next-text=&quot;Next&quot;\r\n                  first-text=&quot;First&quot;\r\n                  last-text=&quot;Last&quot;\r\n                  boundary-links=&quot;true&quot;&gt;\r\n      &lt;\/pagination&gt;\r\n    &lt;\/div&gt;\r\n  &lt;\/div&gt;<\/pre>\r\n\r\n<p><strong>2)AngularJS controller<\/strong><\/p>\r\n\r\n<p>$ vi app\/assets\/javascripts\/mymodule.js.erb<\/p>\r\n\r\n<pre class=\"prettyprint\">\r\nmyModule.controller(&quot;HomeCtrl&quot;, function($scope, flashService, micropostsResource, sessionResource, $filter) {\r\n....\r\n  $scope.submit = function() {\r\n    function success(response) {\r\n      sessionResource.current_user({}, function(response1) {\r\n        var current_user = response1;\r\n        $scope.totalitems = current_user.feed.length;\r\n        feed_items = $filter(&#39;orderBy&#39;)(current_user.feed,&#39;created_at&#39;,true);\r\n        $scope.currentitems = feed_items.slice(start,end);\r\n        flashService.setUser(current_user);\r\n      });\r\n      flashService.push(msg);\r\n      $scope.$emit(&quot;$routeChangeSuccess&quot;);\r\n    }\r\n    function failure(response) {\r\n........\r\n    }\r\n    micropostsResource.create($scope.micropost, success, failure);\r\n  };\r\n.........\r\n  var feed_items = [];\r\n  $scope.maxsize = 5;\r\n  $scope.itemsperpage = 8;\r\n  $scope.currentpage = 1;\r\n  var start = 0;\r\n  var end = start + $scope.itemsperpage;\r\n  sessionResource.current_user({}, function(response) {\r\n    if (response.feed) {\r\n      var tmp_feed = response.feed;\r\n      $scope.totalitems = tmp_feed.length;\r\n      feed_items = $filter(&#39;orderBy&#39;)(tmp_feed,&#39;created_at&#39;,true);\r\n      $scope.currentitems = feed_items.slice(start,end);\r\n    }\r\n  });\r\n  $scope.$watch(&#39;currentpage&#39;, function() {\r\n    start = ($scope.currentpage - 1) * $scope.itemsperpage;\r\n    end = start + $scope.itemsperpage;\r\n    $scope.currentitems = feed_items.slice(start,end);\r\n  },true);\r\n\r\n});<\/pre>\r\n","protected":false},"excerpt":{"rendered":"<p>1)AngularJS template view $ vi app\/assets\/templates\/static_pages\/home.html.erb &lt;div ng-controller=&quot;HomeCtrl&quot;&gt; &lt;div ng-show=&quot;chkSignin().user.id &gt; 0&quot; class=&quot;row&quot;&gt; &lt;div class=&quot;col-sm-4&quot;&gt; &#8230;&#8230;. &lt;\/div&gt; &lt;div class=&quot;col-sm-8&quot;&gt; &lt;h3&gt;Micropost Feed&lt;\/h3&gt; &lt;ol class=&quot;microposts&quot;&gt; &lt;li ng-repeat=&quot;mp in currentitems&quot;&gt; &#8230;&#8230;&#8230;.. &lt;\/li&gt; &lt;\/ol&gt; &lt;pagination total-items=&quot;totalitems&quot; ng-model=&quot;currentpage&quot; max-size=&quot;maxsize&quot; items-per-page=&quot;itemsperpage&quot; class=&quot;pagination-sm&quot; previous-text=&quot;Previous&quot; next-text=&quot;Next&quot; first-text=&quot;First&quot; last-text=&quot;Last&quot; boundary-links=&quot;true&quot;&gt; &lt;\/pagination&gt; &lt;\/div&gt; &lt;\/div&gt; 2)AngularJS controller $ vi app\/assets\/javascripts\/mymodule.js.erb myModule.controller(&quot;HomeCtrl&quot;, function($scope, flashService, micropostsResource, sessionResource, $filter) [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[137],"tags":[35,182,178,172],"amp_enabled":true,"_links":{"self":[{"href":"http:\/\/www.honobono-life.info\/wpeng\/wp-json\/wp\/v2\/posts\/1154"}],"collection":[{"href":"http:\/\/www.honobono-life.info\/wpeng\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.honobono-life.info\/wpeng\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.honobono-life.info\/wpeng\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.honobono-life.info\/wpeng\/wp-json\/wp\/v2\/comments?post=1154"}],"version-history":[{"count":1,"href":"http:\/\/www.honobono-life.info\/wpeng\/wp-json\/wp\/v2\/posts\/1154\/revisions"}],"predecessor-version":[{"id":1155,"href":"http:\/\/www.honobono-life.info\/wpeng\/wp-json\/wp\/v2\/posts\/1154\/revisions\/1155"}],"wp:attachment":[{"href":"http:\/\/www.honobono-life.info\/wpeng\/wp-json\/wp\/v2\/media?parent=1154"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.honobono-life.info\/wpeng\/wp-json\/wp\/v2\/categories?post=1154"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.honobono-life.info\/wpeng\/wp-json\/wp\/v2\/tags?post=1154"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}