1

I'm learning AngularJS and I can't get the data on my mysql database to show up in my view. My codes:

todoController.js

angular
    .module('todoApp')
    .controller('todoController', todoController)
    .config(config);


function config($routeProvider) {
    $routeProvider
    .when('/', {
        controller: 'todoController',
        templateUrl: 'app/views/todo-list.html'
    });
}

function todoController($http, $scope) {
    $http.get('app/endpoints/todo-list.php')
    .then(function successCallback(data) {
        $scope.todos = data;
        console.log(data);
    }, function errorCallback(data) {
        console.log(data);
    });
}

todo-list.html

<table cellpadding="4">
  <tr ng-repeat="t in todos">
    <td>{{ t.name }}</td>
    <td>Remove</td>
  </tr>
</table>

index.html

<!DOCTYPE html>
<html lang="en" ng-app="todoApp">
<head>
    <meta charset="UTF-8">
    <title>AngularJS</title>
</head>
<body>
        <div ng-view><!-- data will be loaded here--></div>

    <script type="text/javascript" src="assets/jquery/jquery.js"></script>
    <script type="text/javascript" src="assets/angular/angular.js"></script>
    <script type="text/javascript" src="assets/angular/angular-route.js"></script>
    <script type="text/javascript" src="app.js"></script>

    <!--controllers-->
    <script type="text/javascript" src="app/controllers/todoController.js"></script>
</body>
</html>

todo-list.php

<?php
require '../../connection.php';
$statement = $db->prepare("SELECT * FROM todo_list");
$statement->execute();
$results=$statement->fetchAll(PDO::FETCH_ASSOC);
echo json_encode($results);

When I check my console, it returns the objects enter image description here

but then the data from my database doesn't show up in the view. My connection is correct. Am I missing something here? Thank you.

1 Answer 1

2

the correct syntax would be -

function todoController($http, $scope) {
    $http.get('app/endpoints/todo-list.php')
    .then(function successCallback(response) {
        $scope.todos = response.data;
        console.log(response);
    }, function errorCallback(reason) {
        console.log(reason);
    });
}

Note - the success function takes the response (data in your case) object. the actual value returned should be response.data (data.data in your case).

Sign up to request clarification or add additional context in comments.

2 Comments

This works, thank you. BTW, is the data in response.data a built-in in javascript?
yes, the whole response is nothing but a javascript object.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.