I'm currently working on some code in Angular which loads a list of blogposts to a website. I have a module and controller, and have an ng-repeat loop set up to iterate through a json which acts as an index for a bunch of html files. However, the tags which should be outputting data from my index object are all empty on the final page.
app.js:
var PersonalApp = angular.module('PersonalApp', [
'ngRoute',
'MasterCtrl'
]);
PersonalApp.config(['$routeProvider',
function($routeProvider) {
$routeProvider.
when('/blog', {
templateUrl: 'partials/blog.html',
MasterCtrl: 'MasterCtrl'
}).
when('/about', {
templateUrl: 'partials/about.html',
MasterCtrl: 'MasterCtrl'
}).
when('/projects', {
templateUrl: 'partials/about.html',
MasterCtrl: 'MasterCtrl'
}).
when('/contact', {
templateUrl: 'partials/about.html',
MasterCtrl: 'MasterCtrl'
}).
otherwise({
redirectTo: '/blog'
});
}]);
MasterCtrl.js:
var MasterCtrl = angular.module('MasterCtrl', []);
MasterCtrl.controller('MasterCtrl', ['$scope', '$http',
function ($scope) {
$.ajax({
url: '../BlogPosts/posts.json',
dataType: 'json',
success: function (bposts) {
$scope.bposts = (bposts);
bposts = JSON.stringify(bposts)
console.log(bposts);
}
});
}]);
blog.html:
<article ng-repeat="posts in bposts" class="post-173 post type-post status-publish format-standard has-post-thumbnail hentry category-uncategorized masonry-brick" style="position: absolute; left: 0px; top: 0px;">
<div class="item-sizer">
<header class="entry-header blog-entry-header">
<div class="entry-data">
<span class="posted-on">
<a ng-href="/../BlogPosts/{{posts.ID}}" rel="bookmark">
<time class="entry-date published updated" datetime="2015-08-20T02:48:02+00:00">{{posts.ID}}</time>
</a>
</span>
</div>
<h1 class="entry-title"><a ng-href="/../BlogPosts/{{posts.ID}}" rel="bookmark">{{posts.Title}}</a></h1> </header><!-- .entry-header -->
{[posts.ID}}
<div class="entry-content">
<p>{{posts.Summary}}<a class="read-more" ng-href="/../BlogPosts/{{posts.ID}}">Continue reading</a></p>
</div><!-- .entry-content -->
</div>
</article>
Here is the console output of my originally jQuery:
{"posts":[{
"ID":"441770384",
"Title":"TestPost",
"Summary":"Doot Doot",
"Thumb":"null"
},{
"ID":"1441835958",
"Title":null,
"Summary":"null",
"Thumb":"null"
},{
"ID":"1441836000",
"Title":null,
"Summary":"null",
"Thumb":"null"
},{
"ID":"1441836039",
"Title":"dfasdf",
"Summary":"null",
"Thumb":"null"
}]}
$http
instead of$.ajax
so you don't have to use$apply()
for each request. Angular doesn't know about$.ajax
so you have to tell it to run digests when you change scope. – charlietfl Sep 22 '15 at 3:56