Last active
March 1, 2016 16:05
-
-
Save AlexanderArmua/ca304b4cb7b38b7c56f6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Esta funcion, se encarga de enviar parametros entre controllers de una manera sencilla, | |
el problema es que es global y todos los controllers tienen acceso a estas variables y la mejor manera, | |
es que se llamen mediante invocacion. | |
*/ | |
var app = angular.module("MyApp", []); | |
//Se crea el factory que responde un objeto vacio, despues se le puede agregar contenido desde los controllers. | |
app.factory("MyService", function() { | |
return { | |
data: {} | |
}; | |
}); | |
// controlador asignado a la ruta /view1 | |
app.controller("Ctrl1", function($scope, $location, MyService) { | |
$scope.goTo2 = function() { | |
//Con solo llamarla por el nombre de la factory ya tenemos acceso. Como si fuera una clase. | |
MyService.data.name = "Paco"; | |
MyService.data.age = 31; | |
$location.url("/view2"); | |
}; | |
}); | |
// controlador asignado a la ruta /view2 | |
app.controller("Ctrl2", function($scope, MyService) { | |
$scope.message = MyService.data.name + " tiene " + MyService.data.age + " años"; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment