My page has a Login section.
Login.html
<ion-view view-title="Login" name="login-view">
<ion-content class="padding">
<div class="list list-inset">
<label class="item item-input">
<input type="text" placeholder="Username" ng-model="data.username">
</label>
<label class="item item-input">
<input type="password" placeholder="Password" ng-model="data.password">
</label>
</div>
<button class="button button-block button-calm" ng-click="login()">Login</button>
</ion-content>
</ion-view>
Upon clicking the login button, the login()
method is executed.
LoginCtrl controller.
.controller('LoginCtrl', function ($scope, LoginService, $ionicPopup, $state, $location, $http) {
$scope.login = function () {
$http({
method: 'GET',
url: 'http://localhost:49431/api/values/GetUserInfo?username=' + $scope.data.username + '&password=' + $scope.data.password + ''
}).then(function successCallback(response) {
if (response.data.Status == true) { // Success
$location.path('/HomePage/').search({ id: '1' }); // Issue here
} else { // Fail
var alertPopup = $ionicPopup.alert({
title: 'Login failed!',
template: 'Username or password is incorrect!'
});
$scope.data.username = "";
$scope.data.password = "";
}
}, function errorCallback(response) {
alert("error");
});
}
})
The code snippet
$location.path('/HomePage/').search({ id: '1' });
is intended to pass the id parameter to the HomePage.Html page. However, the parameter is not passed and the page is not redirected. In short, the location path is not functioning.
HomePageCtrl controller
.controller('HomePageCtrl', function ($scope, HomePageService, $state, $location, $cordovaCamera, $stateParams, $routeParams) {
alert($routeParams.id + $stateParams.id);
}
app.js
.config(function ($stateProvider, $urlRouterProvider) {
$stateProvider
.state('HomePage', {
url: '/HomePage',
templateUrl: 'templates/HomePage.html',
controller: 'HomePageCtrl'
})
.state('login', {
url: '/login',
templateUrl: 'templates/Login.html',
controller: 'LoginCtrl'
});
$urlRouterProvider.otherwise('/login');
});
Question:
How can I successfully pass a parameter to the HomePageCtrl controller from the LoginCtrl controller using $location.path
?
Any assistance would be greatly appreciated.
Thank you.