Search code examples
javascriptangularjsngrouteangularjs-http

AngularJS $http.get with ngRoute how to list details


Maybe someone will help me. I write an app in angularjs, I have a file named list.html which retrieves a list of posts from jsonplaceholder and lists them, with a link to the details of the post. In $ routeParams, I pass the id of the selected one and pick it up. Unfortunately, I have no idea how to download the details of a post and display them in the details.html file. If I want to remove something for example, I write for example $ scope.deletePost as a function and give an id, but how to list details I have no idea.

//routing.js

var myApp = angular.module('myApp', ["ngRoute"])
myApp.config(['$routeProvider',
    function ($routeProvider) {
        $routeProvider
            .when('/test', {
                templateUrl: '/event/example.html',
                controller: 'exampleController'
            }, null)
            .when('/list', {
                templateUrl: '/event/list.html',
                controller: 'exampleController'
            }, null)
            .when('/test-list', {
                templateUrl: '/test/list.html',
                controller: 'testController'
            }, null)
            .when('/test/:id', {
                templateUrl: '/test/details.html',
                controller: 'testController'
            }, null)
    }
]);

//controller.js

angular.module('myApp').controller('testController', function ($scope, $http, $routeParams) {
    $http.get('https://jsonplaceholder.typicode.com/posts').then(function (response) {
        $scope.posts = response.data;
    });

    $scope.id = $routeParams.id;


});

//details.html

<div data-ng-controller="testController">
    {{data}}
</div>

//list.html

<div data-ng-controller="testController">
    <ul>
        <li ng-repeat="post in posts">
          Tytuł: {{post.title}} <a href="#!test/{{post.id}}" >Show</a>
        </li>
    </ul>
</div>

Solution

  • Check out this plunkr.

    You just need to pass the details using ng-href and then catch in the controller using $routeParams. I hope this would help you with what you were looking for.

    var app  = angular.module( 'mainApp', ['ngRoute'] );
    
     app.config( function( $routeProvider ) {
    
     $routeProvider
     .when( '/main', {
        templateUrl: 'list.html',
        controller: 'listCtrl'
      })
      .when('/detail/:id', {
        templateUrl: 'detail.html',
        controller: 'detailCtrl'
      })
      .otherwise({
        redirectTo: '/main'
      });
    });
    
    app.controller( 'listCtrl', function( $scope, $http) {
        $http.get('https://jsonplaceholder.typicode.com/posts')
        .then(function(res){
          $scope.data = res.data;
        })
    
    });
    
    app.controller( 'detailCtrl', function( $scope,$http, $routeParams) {
      $scope.id = $routeParams.id;
       $http.get('https://jsonplaceholder.typicode.com/posts/'+$scope.id)
        .then(function(res){
          $scope.data = res.data;
        })
    });