Sending data

I’m putting together a form and I need to redirect to another view data.

.controller('planesEntrenamientoCtrl', ['$scope','$ionicPlatform', '$location',
 function($scope, $ionicPlatform, $location) {
  $scope.master = {};
  $scope.update = function(user) {
    //Conditions
    // user.name
    // user.apellidos
    // user.genero
    $scope.master = angular.copy(user);
  };}])

you can use:

$state.go('your_state_here');

I’m trying to do so,

.controller('planesEntrenamientoCtrl', ['$scope','$ionicPlatform', '$location',
 function($scope, $ionicPlatform, $location) {
$scope.master = {};
$scope.update = function(user) {
  //Condiciones
  $state.go('perfil', {name : user.name});
  $scope.master = angular.copy(user);
};

}])

but I do not get good results, Error message undefined variable name

.controller('PerfilCtrl', function($scope) {
 console.log("User : "+name);
})

There are a few concepts missing from the code:

  1. When you want to pass data in this way, you send it as a parameter in the $state.go() call, but you also need to retrieve it from $state when the destination controller picks it up.

If you inject $state into the PerfilCtrl controller after $scope, you can access any parameters passed this way by looking at $state.params - so in your case, $state.params.name should give you access to the name parameter

(I’m assuming you haven’t put all of the controller code for planesEntrenamientoCtrl in, as I can’t see $state being injected there, so your assignment would fail)

  1. When you define the states and URLs in your app.js file, you have to make sure the URL for the state includes that named parameter, otherwise it gets ignored.

In you case, the URL should look something like '/perfil/:name'

  1. Be careful that there are limits to what’s allowed in the state data. In particular, your values can’t contain any '/' characters as that confuses the router, so $state.go('perfil, {name: 'one/two'}) would cause problems.
1 Like