I have some issue with AngularJS scope and location. Here is an example :
function CreateAccountCtrl($scope, $http, $location) {
...
$http.post(url,$scope.form).
success(function(data){
$location.path('/'); // I need to transfert data to the location
}
}
My problem is : I want to transfer the data to the / controller, I thought to use the rootScope but I don't think it is the best way to do this.
Any idea ?
You can use $rootScope to send data between controllers. $routeParams doesn't allow send complex data structures. Let's see it.
Assign returned data of success callback to variable in $rootScope. Navigate to the AccountController.
function CreateAccountCtrl($scope, $http, $location,$rootScope ) {
...
$http.post(url,$scope.form).
success(function(data){
$rootScope.accountInfo = data;
$location.path('/account');
}
}
Configure the route in the $routeProvider:
$routeProvider.when('/account', {
template: 'account.html',
controller: AccountCntl
});
In the AccountController can access the data on $rootScope.
function AccountCtrl($scope, $rootScope) {
$scope.accountInfo = $rootScope.accountInfo;
}
Use the $routeParams service.
function CreateAccountCtrl($scope, $http, $location) {
...
$http.post(url,$scope.form).
success(function(data){
$location.path('/account/' + data.accountId);
}
}
Configure the route in the $routeProvider:
$routeProvider.when('/account/:accountId', {
template: 'account.html',
controller: AccountCntl
});
Your AccountController can now access the data:
function AccountCtrl($scope, $routeParams) {
$scope.accountId = $routeParams.accountId;
}