0

I an using spring and hibernate with angularjs.. and i am fetching data from database but i want to get my data in angular function..

Here is my angular function

   artle = angular.module('myApp', []);

   artle.controller('myCtrl', function($scope) {
       $scope.class = "div0f";
       $scope.changeClass = function() {
           if ($scope.class === "div0f")
               $scope.class = "div0p";
           else
               $scope.class = "div0f";
       };
       $scope.Unsub = function() {
           if ($scope.class === "div0p")
               $scope.class = "div0g";
       };
   })

I want to get a value from database to use in this function in if else condition.

Please suggest me. Thnx in advance

Manish Kr. Shukla
  • 4,447
  • 1
  • 20
  • 35
Nadeem Ahmed
  • 371
  • 2
  • 8
  • 24

1 Answers1

0

As said BetaRide, you'll do it in the callback of your JAX call. In Angular you'll ask for some action on a Spring REST Endpoint. When Spring is done, it will send you the answer. If the answer is a success then it will enter the success callback method, the error one if there is any error.

$http({
    url: "http://example.com/rest/test",
    method: "POST",
    data: {"toto":"titi"}
}).success(function(data, status, headers, config) {
    $scope.data = data; // Here you can call your method with data
}).error(function(data, status, headers, config) {
    $scope.status = status; // Managae error
});

You have to manage your $http injection

angular.module('myApp.controllers', []).
controller('ImagesCtrl', ['$scope', '$http', function ($scope, $http) {
  // ...
});
RPresle
  • 2,436
  • 3
  • 24
  • 28
  • REST is a norm of how to build Web services. Refer to [this for a description](http://www.oracle.com/technetwork/articles/javase/index-137171.html). In Java it refers to the JAX-RS spec and is usually used in Spring with Jersey. – RPresle Jun 23 '15 at 07:16
  • So while using angular js for my view, should i be using Rest ? Or is there an alternative to rest api ? – Nadeem Ahmed Jun 24 '15 at 11:38
  • Lets go back to the beginning. You have two part in your application : Front and Back. The Back is Spring with database and the front is your angular. You want Angular and Spring to communicate. To communicate there are several norms. Rest is the one that go with angular (advice). To ask for some data to Spring you need to do HTTP request via Ajax call on Webservices. Then, Spring will send you back data that will be manage by angular. Please refer to [this for a example of architecture](http://java.dzone.com/articles/web-app-architecture-spring-0) – RPresle Jun 24 '15 at 14:10
  • if i want to delete a record from database then whether i need to pass id of that record in angular function or it can be done by without passing id? Because i dont want to do any work in my html code. – Nadeem Ahmed Jun 25 '15 at 06:31
  • and thanx for the suggestion – Nadeem Ahmed Jun 25 '15 at 06:31
  • You may not consider HTML/angular/spring but just front/back. As Angular is javascript it is easy for it to access data contained in your HTML as the id of the object you want to delete. If you are discovering web development with Ajax call please do refer to a full tutorial. The flow is this one: user act on HTML, angular catch the event, create an ajax request with some data (maybe Id) and call spring web service, Spring do the job with its database and send back the request to angular, on receiving the answer angular execute the success function and do the conresponding work – RPresle Jun 25 '15 at 11:56
  • i got wt u r saying...but i want to know that is it necessary to pass id in my function like onclick="delete(id)" or it can be done like onclick="delete()" ? – Nadeem Ahmed Jun 26 '15 at 09:22
  • You can choose the way you want it to work. or you can do onclick="delete(id)" (quite old fashioned), the jquery way would [apply an event on the button](https://api.jquery.com/click/) and get the id from the HTML hidden input, button's id) and angular have special directive [ng-click](https://docs.angularjs.org/api/ng/directive/ngClick#!). Use the one you prefer =) – RPresle Jun 26 '15 at 09:32
  • i am using angular with spring and hibernate.. and i am new in this..i am deleting a record from database with id 1 using a button ...please write a code to do this ... i am unable to do – Nadeem Ahmed Jun 26 '15 at 09:49
  • Consider giving me what you already have please. HTML, and js so I can figure out where you are. You should consider doing a full Angular tutorial to discover the technology and the javascript. Start doing something by searching the web on how to do it. I'll be happy to help you if you are blocked – RPresle Jun 26 '15 at 09:57
  • here is my angular code – Nadeem Ahmed Jun 26 '15 at 10:03
  • And it is my html code ...
    – Nadeem Ahmed Jun 26 '15 at 10:04
  • i am working on a subscribe button ...when i click on subscribe then it change to unsubsribe and the id is inserted to database in subscribe table and when i click on unsubscribe then it change to subscribe and the id is deleted from table in db – Nadeem Ahmed Jun 26 '15 at 10:06
  • and i have delete method in my spring controller . @RequestMapping("/delete/{employeeId}") public String deleteEmplyee(@PathVariable("employeeId") Integer employeeId) { employeeManager.deleteEmployee(employeeId); return "redirect:/"; } – Nadeem Ahmed Jun 26 '15 at 10:54
  • Please remove these unreadable comments and updates your question to add the content in a more readable formatted way. Explain what is your new problem based on this code. Consider I'm a fresh new user try to explain your situation and where exactly is your problem. Thks – RPresle Jun 26 '15 at 12:12