Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save AlexanderArmua/ca304b4cb7b38b7c56f6 to your computer and use it in GitHub Desktop.
Save AlexanderArmua/ca304b4cb7b38b7c56f6 to your computer and use it in GitHub Desktop.
/*
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