0

I'm new to AngularJS, And just started learning form various sites like (W3school,angularJS). I am trying to practice examples what ever they have mentioned in the tutorials. Here I'm simply trying to display values in front-end using angularJS but it's not not working for me. There is no error in console nor in spring boot.

Values are coming from database(postgresql), and using Spring Boot(STS).

Below is the app.js file code:

var myApp = angular.module('myApp', []);
myApp.controller('myCtrl', function($scope, $http) {
    $scope.test='this is a test';
    $http.get("http://localhost:7000/allusers").then(function(response){       
        $scope.user = response.data.users;
       /* alert("User Name: "+user.user_name);*/
    },
    function(errResponse){
            console.error('Error while fetching Users');
            deferred.reject(errResponse);
            $scope.error='error getting'
    });
});

Below is the NewFile.html code:

<!doctype html>
<html lang="en" data-ng-app="myApp">
<head>
    <meta charset="utf-8">
    <title>Google Phone Gallery</title>
    <!-- <script src = "angular.min.js"></script> -->
    <script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
    <!-- <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.4/angular.js"></script> -->
    <script src ="../../JS/app.js"></script>
    <!-- <script src = "angular-resource.min.js"></script> -->

 </head>

<body data-ng-controller="myCtrl"> 

<h1>{{naber}}</h1>
<h1>a line</h1>
<h1>{{user}}</h1>
<h1>a line2</h1>
<!-- <div data-ng-controller="myCtrl">
{{user.user_name}} {{user.user_password}}</div>-->
<br><hr>
<div data-ng-repeat="article in articles">{{user.user_name}} {{user.user_password}}</div>

</body>
</html>

usercontroller.java(rest controller):

package com.example.demo.controller;


import java.util.List;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.example.demo.service.UserService;
import com.example.demo.vo.UserDtlsVO;

@RestController
@EnableAutoConfiguration
@RequestMapping("/")
public class UserController {

    private final Logger LOG = LogManager.getLogger(UserController.class);

    @Autowired
    private UserService userService;

     /*@RequestMapping("/")
     public String index(Model model){
     System.out.println("Looking in the index controller.........");

     return "index.html";
      }*/

    @RequestMapping(value="allusers", produces = "application/json" , method=RequestMethod.GET)
    public ResponseEntity<List<UserDtlsVO>> getAll() {
        System.out.println("Inside the allusers rest controller: ");
        List<UserDtlsVO> users = userService.curdusers();
        HttpHeaders headers = new HttpHeaders();
        ModelAndView modelAndView = new ModelAndView("success");
        if (users == null || users.isEmpty()){
            LOG.info("no users found");
            return new ResponseEntity<List<UserDtlsVO>>(HttpStatus.NO_CONTENT);
        }
        //modelAndView.addObject("users",users);
       // headers.setLocation(URI.create("NewFile.html"));
        System.out.println("Just about to return object");
        return new ResponseEntity<List<UserDtlsVO>>(users, HttpStatus.OK);
    }

}

I'd like to understand why the values aren't showing. I would be happy if you could suggest me a good tutorial(or any free pdf file).

phuzi
  • 12,078
  • 3
  • 26
  • 50
  • what output it is showing? and check what value you are getting in $scope.user – Rakesh Burbure Jan 10 '18 at 13:07
  • Does `{{test}}` render correctly? I'd suggest trying the app without backend, i.e. replacing `$http.get("http://localhost:7000/allusers")` with `$q.when([{id: 1}, {id: 2}])` (or `$q.when({id: 'firstUser})`) after injecting `$q` to the controller. Also, you seem to be requesting all users, but storing the result in `$scope.user`. You can try to see what it looks like with `{{user | json}}`. Once you get up and running, I'd suggest taking a look at John Papa's style guide: https://github.com/johnpapa/angular-styleguide/blob/master/a1/README.md – jsruok Jan 10 '18 at 13:56
  • while using {{test}} it show just ' this is a test ' – Parth Doshi Jan 12 '18 at 11:06

0 Answers0