Sending data to another app modul (another page) using a dblclick function in angularjs

I have two pages in my angularjs application, I get some data from database to show in a table, then i doubleclick on a row and go to another page to show more details. The problem consists on how to send data from the first page to the second one to show them knowing that i have all the data i need to show in my variable “branchs” that i showed some of them in the first page, here is a part of my code :

var app= angular.module('MyApp' , []);
app.controller('Ctrl', function($scope, $http){
 $http.get('http://localhost:8080/Test_WS/test/branchs/brnch')
 .then(function(response) {
     $scope.branchs = response.data;
 })
 .then(function(response) {
 $scope.details = function (b) {
    window.location = 'http://localhost:8080/TestProject/CreateBranch.html'; 
 };
 });

});

Hi @saidaouak,

If you’re wanting to share data between two or more controllers (or components) in an Angular 1 app, then you can create a service that they can require as a dependency and do the data fetching there.

From your code example, though, it looks like you’re redirecting to a separate page, so any state in the current app will be lost.

1 Like

Hi @fretburner !
I ve created a service but getdetails method doesn’t work , i tested by setting a value to my var v inside the service and it works, but when i want to set some value to my service in the controller it fails to set data that i want to send , look at this code :

  var app= angular.module('MyApp' , []);
  app.controller('Ctrl', function($rootScope ,$scope, $http, detail){
$http.get('http://localhost:8080/Test_WS/test/branchs/brnch')
 .then(function(response) {
	$scope.branchs = response.data; 
	
 })
 
 .then(function(response) {
  $scope.details = function (b) {		 
	window.location = 'http://localhost:8080/TestProject/CreateBranch.html';  
	detail.setdetails("xx");   // or detail.setdetails(response.data);
 };
});
});

app.service('detail', function() {

 return {
	 setdetails: function(value) {
		 v = value;
	 },
	 getdetails: function(){
		 return v;
	 }
 };
});

other app file :

  var app2= angular.module('MyApp2' , ['MyApp']);
 app2.controller('Ctrl2', function ($rootScope, $scope, detail) { 
	
 $scope.test = detail.getdetails();
});

As I said before, it looks like you’re redirecting to a separate page, with a second Angular app on it, is that right? If so, is there any reason for doing that, as opposed to using routing within your app to display a different view?

1 Like

@fretburner
Yes, that’s right, i’m redirecting to another page with a second angular app, i did it for no reason just because i’m a beginner with angularjs and i didn’t think at using views… so will that be easy to display my data with factories without problem using views ?

Hi!
Now I’m using views but i still have a problem that is I don’t know how to change the view when i dblclick on a table row, i mean how to send the path of the view to show within the dblclick function !

Require the $location service in your view controller, and call $location.path() passing in the route that you want to navigate to. You can then trigger this from your view with an ng-dblclick attribute.

yees i succeed to access my new view, but there is still a small problem, when the view is shown it repeats the content of my home page :confused:
thanks a lot @fretburner for replying :slight_smile:

It might be worth having a look at the examples in this tutorial to double check that you’ve got everything set up correctly for the views/routing.

If you’re still stuck after that, could you create a simple example in something like Plunker? That’d make it a lot easy to help you.

it’s okey , i solved the problem, i simply change my files, it was an internal problem … thanks a lot :slight_smile:

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.