Ionic1 app gets reload on clicking a button - ionic-framework

I have an ionic app with side-menu. My requirement is to have a Login Page before side-menu page.
By clicking on login button first time application gets reload, on clicking second time navigates to the side Menu page.
Here is my code
app.ts
config(function ($stateProvider, $urlRouterProvider) {
$stateProvider
.state('login', {
url: '/login',
templateUrl: 'templates/login.html',
controller: 'loginCtrl'
})
.state('app', {
url: '/app',
abstract: true,
templateUrl: 'templates/menu.html',
controller: 'AppCtrl'
})
.state('app.home', {
url: '/home',
cache: true,
views: {
'menuContent': {
templateUrl: 'templates/home.html',
controller: "homeController"
}
}
})
<ion-view>
<ion-content>
<div class="login-main-heading">
<h3 class="all-elements-center login-heading">Welcome</h3>
<div class="form-elements responsive-sm">
<h5 class="all-elements-center login-heading">Login</h5>
<form action="">
<div class="list list-inset">
<label class="item item-input">
<i class="icon ion-person placeholder-icon"></i>
<input type="text" placeholder="User Name">
</label>
<label class="item item-input">
<i class="icon ion-lock-combination placeholder-icon"></i>
<input type="password" placeholder="Password">
</label>
<div class="responsive-sm">
<button class="button button-large button-assertive button-login">
Login
</button>
<button class="button button-large button-assertive button-signup">
Sign Up
</button>
</div>
<div>
<button class="button button-block button-positive button-facebook- login" ng-click="login()">
Login
</button>
</div>
</div>
</form>
</div>
</ion-content>
</ion-view>
login-controller.js
angular.module('starter')
.controller('loginCtrl', function ($scope, $state,$ionicSideMenuDelegate) {
$scope.login = function () {
$state.go('app.home');
}
});
app-controller.js
angular.module('starter.controllers', [])
.controller('AppCtrl', function ($scope, $state) {
});

Related

Error:Could not route into another page in ionic

I can't navigate to it another template
its shows me error above
and i also tried ui-sref to go signup templete
Here My Starter app Structure--
I have templates of
Login.html
Signup.html
dash.html
Here my index page
<body ng-app="starter">
<ion-tab title="Home" >
<ion-nav-view name="menuContent"></ion-nav-view>
</ion-tab>
<ion-nav-bar >
<ion-nav-back-button class="button-icon ion-ios7-arrow-back">
</ion-nav-back-button>
</ion-nav-bar>
<ion-nav-view > </ion-nav-view> -->
<ion-header-bar class="bar-stable">
<h1 class="title">Login</h1>
</ion-header-bar>
<ion-nav-view></ion-nav-view>
and my app.js is
.state('login', {
url: '/',
templateUrl: 'templates/login.html',
controller: 'LoginCtrl'
})
.state('Fourm' ,{
url : '/SignUp',
templateUrl: 'templates/SignUp.html',
controller :'SignCtrl'
})
$urlRouterProvider.otherwise('/');
and my Controller.js
.controller('LoginCtrl', function($scope, LoginService, $ionicPopup, $state) {
$scope.data = {};
$scope.Login = (function() {
LoginService.loginUser($scope.data.username, $scope.data.password).success(function(data) {
//var Popup = $ionicPopup.alert({title : 'Login success'})
alert("Welcome User")
$state.go('tab');
}).error(function(data) {
var alertPopup = $ionicPopup.alert({
title: 'Login failed!',
template: 'Please check your credentials!'
});
});
})
)
.controller('SignCtrl' , function($scope, $ionicPopup, $state) {
console.log("in cotrl")
$scope.SignUp = (function()
{
alert("user")
$state.go('Fourm');
});
})
and my LoginTemplate
<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>
<<button class="button button-block button-assertive" ng-click="SignUp()" >SignUp</button>
</ion-content>
</ion-view>
1) Not a good way to put ion-tab in index.html unless you want tab in all page in your app.
2) ion-tab define single tab which should be in ion-tabs container.
3) In angular ui router for tab container we have to define route with abstarct true.
For mor information you can see here
I had created simple codepen. Which will help you.
So in templates/index.html
<ion-nav-bar class="bar-positive">
<ion-nav-back-button>
</ion-nav-back-button>
</ion-nav-bar>
<ion-nav-view></ion-nav-view>
templates/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>
templates/tabs.html
<ion-tabs class="tabs-icon-top tabs-positive">
<ion-tab title="Login" icon="ion-home" ui-sref="tabs.login">
<ion-nav-view name="login-tab"></ion-nav-view>
</ion-tab>
<ion-tab title="Signup" icon="ion-ios-information" ui-sref="tabs.signup">
<ion-nav-view name="signup-tab"></ion-nav-view>
</ion-tab>
</ion-tabs>
//instead of ui-sref we can use ng-click => $state.go('tabs.signup')
too.
and in app.js
$stateProvider
.state('tabs', {
url: "/tab",
abstract: true,
templateUrl: "templates/tabs.html"
})
.state('tabs.login', {
url: "/login",
views: {
'login-tab': {
templateUrl: "templates/login.html",
controller: 'LoginCtrl'
}
}
})
.state('tabs.signup', {
url: "/signup",
views: {
'signup-tab': {
templateUrl: "templates/signup.html"
}
}
})
$urlRouterProvider.otherwise("/tab/login");

popover doesn't work in a modal-ionic

my modal:
<div class="modal">
<!-- Modal header bar -->
<ion-header-bar class="bar-secondary ">
<h1 class="title ">New Task</h1>
<button class="button button-clear button-positive " ng-click="closeNewTask() ">Cancel</button>
</ion-header-bar>
<!-- Modal content area -->
<ion-content>
<form ng-submit="">
<div class="list ">
<div class="item">
<label class="item item-input ">
<input type="text " placeholder="What do you need to do? ">
<button type="button" class="button button-icon" ng-click="openPopover($event)">
<i class="icon ion-information-circled red font-size-25"></i>
</button>
</label>
</div>
</div>
<div class="padding ">
<button type="submit" class="button button-block button-positive ">Create Task</button>
</div>
</form>
</ion-content>
</div>
this is my modal it works fine but the "icon information" inside the modal doesn't work ... i put a $ionicPopover in the controller and the functions
var template = '<ion-popover-view><ion-header-bar> <h1 class="title">My Popover Title</h1> </ion-header-bar> <ion-content> Hello! </ion-content></ion-popover-view>';
$scope.popover = $ionicPopover.fromTemplate(template, {
scope: $scope
});
$scope.openPopover = function($event) {
$scope.popover.show($event);
};
everythink works good out of the modal , but inside not
$scope.openPopover = function($event) {
$scope.popover.show($event);
};
change it to
$scope.openPopover = function(e) {
$scope.popover.show(e);
};

not able to open new page on button click

Hi Developing an Mobile app using Ionic Framework.
I know its very basic question but i m new to this.
In the Index.html is having two buttons 1 is for Login and other for SignUp
index.html
<script src="js/app.js"></script>
<script src="js/controllers.js"></script>
<script src="js/directives.js"></script>
<script src="js/services.js"></script>
<script src="js/routes.js"></script>
</head>
<body ng-app="starter" ng-controller="MainCtrl">
<ion-pane>
<div class="bar bar-header bar-calm">
<h1 class="title">Heading</h1>
</div>
<ion-content paging="true" class="has-header" >
<div>
<img src="img/Logo.jpg" width="100%" height="auto" style="display:block;margin-left:auto;margin-right:auto;" />
<img src="img/Team.jpg" width="70%" height="auto" style="display:block;margin-left:auto;margin-right:auto;" />
</div>
<div ng-cntroller="loginCtrl">
<button id="btnLogin" class="icon icon-right ion-log-in left button button-positive button-block " style="border-radius:15px 15px 15px 15px;" ng-click="login()">Login</button>
</div>
<div ng-cntroller="signupCtrl">
<button id="btnSignUp" class="button button-positive button-block icon icon-right ion-person left" style="border-radius:15px 15px 15px 15px;">SignUp</button>
</div>
<div class="bar bar-footer bar-calm">
<div class="title">Copyright <strong>#</strong></label>
</div>
</ion-content>
</ion-pane>
On login click it open the Login.html page similar way click on sign up it open Signup.html
app.js
angular.module('starter', ['ionic','starter.controllers','starter.routes','starter.services','starter.directives'])
.run(function($ionicPlatform) {
$ionicPlatform.ready(function() {
if(cordova.platformId === 'ios' && window.cordova && window.cordova.plugins.Keyboard) {
// Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
// for form inputs)
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
// Don't remove this line unless you know what you are doing. It stops the viewport
// from snapping when text inputs are focused. Ionic handles this internally for
// a much nicer keyboard experience.
cordova.plugins.Keyboard.disableScroll(true);
}
if(window.StatusBar) {
StatusBar.styleDefault();
}
});
})
routes.js
var app = angular.module('starter', [])
app.config(function($stateProvider,$urlRouteProvider){
$stateProvider
.state('text', {
url: '/page1',
templateUrl: 'templates/text.html',
controller: 'textCtrl'
});
.state('login', {
url: '/page2',
templateUrl: 'templates/login.html',
controller: 'loginCtrl'
});
.state('signUp', {
url: '/page3',
templateUrl: 'templates/signup.html',
controller: 'signupCtrl'
});
.state('chats', {
url: '/page4',
templateUrl: 'templates/chats.html',
controller: 'chatsCtrl'
});
$urlRouteProvider.otherwise('/page1')
});
controllers.js
var app = angular.module('starter')
app.controller('textCtrl',function($scope){
});
app.controller('loginCtrl', function ($scope) {
$scope.login = function(){
alert("Hi")};
});
app.controller('signUpCtrl', function ($scope) {
});
app.controller('chatsCtrl', function ($scope) {
});
login.html
<ion-view title="Login" id="page2" class=" ">
<ion-content padding="true" class="has-header">
<form id="login-form1" class="list ">
<ion-list id="login-list1" class=" ">
<label class="item item-input " id="login-input1">
<span class="input-label">Username</span>
<input type="text" placeholder="">
</label>
<label class="item item-input " id="login-input2">
<span class="input-label">Password</span>
<input type="password" placeholder="">
</label>
</ion-list>
<div class="spacer" style="height: 40px;"></div>
<a ui-sref="chats" id="login-button3" style="border-radius:15px 15px 15px 15px;" class=" button button-calm button-block ">Log in</a>
<a ui-sref="signup" id="login-button4" class=" button button-positive button-block button-clear ">Or create an account</a>
</form>
</ion-content>
</ion-view>
You need to define what the login button should do. Two ways to go
Either do
<button id="btnLogin" ui-sref="login">Login</button>
or do this on ng-click of the button like
$scope.login = function(){
$state.go('login');
};
Make sure $state is injected into your controller.
using this link will help to solve my issue
http://www.joshmorony.com/user-authentication-with-ionic-and-parse-part-1-email-login/

IONIC Pull to refresh

Just try to implement pull to refresh from ionic framework, but it does not work, the icon just keep showing and nothing happen.
Anyone can point where is the problem?
Here is the code.
lists.html
<ion-header-bar align-title="center" class="bar-stable">
<h1 class="title">Latest Items</h1>
<button class="button button-clear button-positive" ui-sref="addlist">New</button>
</ion-header-bar>
<ion-view title="Terbaru">
<ion-content>
<ion-refresher on-refresh="doRefresh()">
pulling-text="Pull to refresh..."
</ion-refresher>
<label class="item item-input">
<i class="icon ion-search placeholder-icon"></i>
<input type="text" name="dash.search" value="" placeholder="Search">
</label>
<div class="list card" ng-repeat="x in data" type="item-text-wrap" href="#/tab/chat/{{x.id}}">
<div class="item item-avatar">
<img data-ng-src="data:image/png;base64,{{x.photopath}}">
<h2>{{x.title}}</h2>
<p>{{x.tgl}}</p>
</div>
<div class="item item-body">
<img class="full-image" data-ng-src="data:image/jpeg;base64,{{x.imagepath}}">
<p>
{{x.descr}}
</p>
<p>
1 Like
5 Comments
</p>
</div>
</div>
</ion-content>
</ion-view>
controller.js
angular.module('ionicApp.controllers', [])
.controller('AddListCategoryCtrl', function($scope, $http) {
var xhr = $http({
method: 'post',
url: 'http://www.mywebsite.com/api/listCat.php'
});
xhr.success(function(data){
$scope.data = data.data;
});
$scope.doRefresh = function(){
$http.get('http://www.mywebsite.com/api/lists.php')
.success(function(data){
$scope.data=data.data;
console.log($scope.data);
})
.finally(function(){
$scope.$broadcast('scroll.refreshComplete');
$scope.$apply()
});
}
});
There is an error in your code. Pulling text should be an attribute of ion-refresher.
This is the correct code:
<ion-refresher on-refresh="doRefresh()" pulling-text="Pull to refresh...">
</ion-refresher>
You added the closing tag before the pulling-text attribute.

Ionic: close the side menu but dont set state to root

I want to use a side menu:
<ion-side-menus enable-menu-with-back-views="true">
<ion-side-menu-content>
<ion-nav-bar class="bar-positive" align-title="center">
<ion-nav-back-button>
</ion-nav-back-button>
<ion-nav-buttons side="left">
<button class="button button-icon button-clear ion-navicon" menu-toggle="left">
</button>
</ion-nav-buttons>
</ion-nav-bar>
<ion-nav-view name="menuContent"></ion-nav-view>
</ion-side-menu-content>
<ion-side-menu side="left" width="300">
<ion-content>
<div class="list">
<a class="item item-icon-left left no-border" ui-sref="app.home" menu-close>
<i class="icon menu-icon ion-home"></i>
<h2>Hom</h2>
</a>
<a class="item item-icon-left left no-border" ui-sref="app.account" menu-close>
<i class="icon menu-icon ion-person"></i>
<h2>Item 1</h2>
</a>
<a class="item item-icon-left left no-border" ui-sref="app.orders" menu-close>
<i class="icon menu-icon ion-android-list"></i>
<h2>Item 2</h2>
</a>
</div>
</ion-content>
</ion-side-menu>
</ion-side-menus>
As you can see im using the menu-close directive because i want the side menu to hide when i clicked on an item. The problem is that using this menu-close directive the state of the selected item is set to be the root. That means if i press the back button i wont get navigated back.
How can i close the menu when i click on an item without setting the selected view to be the root of the state?
I solve this doing the following in my menu item controllers:
angular.module("App.Home", [])
.config(function ($stateProvider, $urlRouterProvider) {
$stateProvider.state("app.home", {
url: "/home",
views: {
"menuContent": {
templateUrl: "templates/menu_home.html",
controller: "HomeController"
}
}
});
})
.controller("HomeController", function($scope, $ionicSideMenuDelegate) {
$ionicSideMenuDelegate.toggleLeft(false);
});
EDIT
The following is an even better solution:
Just add a controller to your side menu:
<ion-side-menu side="left" width="300" ng-controller="MenuController">
And this to your controller:
angular.module("App.Menu")
.controller("MenuController", function($scope, $ionicSideMenuDelegate) {
$scope.toggleMenu = function() {
if($ionicSideMenuDelegate.isOpenLeft()) {
$ionicSideMenuDelegate.toggleLeft(false);
} else {
$ionicSideMenuDelegate.toggleLeft(true);
}
}
});
In your menu items you just need to call:
<a class="item item-icon-left item-left item-no-border" ui-sref="app.home" ng-click="toggleMenu()">
and everything is working like a charm.