Having trouble connecting angular repository data with promise

I have been trying to implement the repository pattern in my Angular application. I can see that the JSON data is successfully retrieved over the network when I call my repository, but I am facing issues with binding it to my view.

I have injected the repository into the module, so what could be the issue here?

blogApp.factory('blogEntryRepository', function ($http, $q) {
    return {
        get: function() {
            var deferred = $q.defer();
            $http.get('/blogentry').success(deferred.resolve).error(deferred.reject);
            return deferred.promise;
        }
    }
});


blogApp.controller("HomeCtrl", function($scope, blogEntryRepository) {
    blogEntryRepository.get().then($scope.blogEntries = blogEntryRepository);
});

<div>
    home  
    <div ng-repeat="blogEntry in blogEntries">
        <div>Asdf
            {{blogEntry.title}}
        </div>
    </div>
</div>

On the page, I see the following output:

home
Asdf
{}

Here is the JSON response:

[{"title":"Launched an AngularJS 1.2 site!","date":"2014-05-21T00:00:00"},{"title":"Title2","date":"2014-05-22T00:00:00"}]

The code is enclosed within an ng-app tag and there is an ng-view tag on the page as well.

I have confirmed that I am able to bind to a hardcoded variable successfully.

Using Angular 1.2

Answer №1

experiment

app.controller("HomepageController", function($scope, blogRepo) {
    blogRepo.fetch().then(function(result) {
        $scope.entries = result;
    });
});

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

When the PHP response is received by AJAX, an error occurs due to a failed JSON parsing request

Every time I try to run my small JavaScript code with an AJAX call to PHP, it keeps coming back with a JSON parser error. In the PHP code, I can see that the JSON is populated with an array like this: json encode: {"Year":"2012","Make":"Ford","Model":"Tau ...

Mishandling of string interpretation

I'm having trouble converting ANSI color codes from console output into HTML. While I discovered a script that can handle this task, I am struggling to make it parse the strings within node js properly. Even when I attempted to JSON.stringify it to in ...

create a sapper route using JSON data

Beginning with the official Sapper template, I am keen on implementing export default as recommended by eslint: export default function get(_, res) { res.writeHead(200, { 'Content-Type': 'application/json', }); res.end(conte ...

When trying to convert to JSON in node, the process fails. However, the data can still be

I am currently working on converting an array to JSON in order to send it to a client. The data I see in the console is as follows: [ NL: [ true, true, true, true, true, true, true, true, true, true, true, true ], ...

AngularJS: The blend of bo-bind, bindonce, and the translate filter

I am currently working with angular 1.2.25, angular-translate 2.0.1, angular-translate-loader-static-files 2.0.0, and angular-bindonce 0.3.1. My goal is to translate a static key using bindonce. Here is the code snippet I have: <div bindonce> < ...

Is there a way for me to redirect back to the original path?

Whenever I utilize <Redirect to="<same_path>" />, a warning pops up: Warning: You tried to redirect to the same route you're currently on: "<path>". In one of my Components, I trigger another Component that prompts for either submis ...

Which is the better option for opening a link in a button: using onclick or href?

What is the most effective way to open a link using a button? <button type="button" onclick="location='permalink.php'">Permalink</button> <button type="button" href="index.php">Permalink</button> ...

Displaying an image within a textarea using JS and CSS

Could someone help me create a form with textareas that have small images on them? I want clicking on the image to call a function fx(). Can anyone code this for me using JavaScript/jQuery/CSS? In the image below, clicking on "GO" will call Fx() I attemp ...

sorting in React table not functioning properly for computed column

I have a column titled 'EV/Resource ($ per oz)' that appears to not sort correctly in my react table. Instead of sorting in ascending or descending order, it randomizes the table sort. I'm unsure if I am handling this as a "calculated column ...

Gradient on multiple faces in Three.js

I'm currently struggling with creating an HSV cylinder using three.js. I am facing difficulties in properly mapping the gradient to the faces of the cylinder. Initially, I attempted to create my object in this manner: https://i.sstatic.net/WboAE.png ...

Utilizing AngularJS to display an array of user inputs

Hey there, hope all is well with you! I have been working on an HTML form that looks like this: <div class="table-responsive " > <table class="table table-bordered"> <tr ng-repeat="x in [1, 2, 3, 4, 5]"> <td> <td align="center" ...

Separate the information into different sets in JavaScript when there are more than two elements

Upon extraction, I have obtained the following data: ╔════╦══════════════╦ ║ id ║ group_concat ║ ╠════╬══════════════╬ ║ 2 ║ a ║ ║ 3 ║ a,a ...

Looping through NavItems component using JavaScript or Angular

My Angular project includes a navbar component with an app sidebar that has a navItems attribute. Below is the content of my navBar: <app-header style="background-color : #e65100;" [fixed]="true" [navbarBrandFull]="{ src: &a ...

What sets canvas and webgl renderer apart in the world of three.js?

Attempting to showcase a sphere using three.js, but encountering issues when rendering with canvasRenderer due to the appearance of grey lines on the sphere. View the code here: http://jsfiddle.net/jzpSJ/ See the screenshot here: However, when rendering ...

The selection button's threshold

Welcome to my website design project! I have implemented image buttons for my web page and want to restrict the number of images a user can select. If a user tries to navigate away after selecting more than 3 images, I wish to display an alert message. Her ...

Header for AngularJS blog post worldwide

Is it possible to create a universal header in AngularJS that functions similarly to jQuery? For example, something like the code below: $.ajaxSetup({ beforeSend: function (xhr) { xhr.setRequestHeader('Requested-With', 'XMLHttp ...

saving the JSON information for dropdown options within a variable

Greetings everyone I have successfully retrieved the options for my select tag in json format and used this code to populate the select tag: var mySelect = $('#'+select_id) $.each(response, function(key,val){ $('<option/>&apo ...

Is there a way to guide the user to a different page while still inside a getJSON function?

After making an external call to a JSON file, I am facing an issue where if the JSON data is null (possibly due to expired user session), the user should be redirected to the logout.php page. However, in my current implementation, instead of redirecting, t ...

Native Android application connected to a back-end service for user authentication

I am looking to develop a native Android application for a particular website. The primary challenge lies in figuring out how to implement the login functionality on the Android app, without altering the existing webservice site. The goal is for the Andro ...

Encountering a php error when decoding multiple JSON files due to an unexpected end of file

Hey there! I'm currently working on a project where I need to load JSON files from a specific folder and then decode them to populate a database. Below is the code snippet I am using for this task: <?php $con = mysqli_connect("localhost", "root", ...