AngularJS problem with backing to home page - javascript

This is how i config the home page:
$stateProvider.state('home', {
url: '',
templateUrl: 'static/templates/home.html',
controller: 'homeCtrl'
});
And above this working nice.
here you got to see my nav:
Home
Add New
I have another route named: add
$routeProvider
.when('/add', {
templateUrl: 'static/templates/add.html',
controller: 'addCtrl'
});
Above everything working fine but the problem is: when i click on href="#!/add">Add New</a> it works good to navigate me to add.html page but when i click on Home the page not back to home, it still stay in add even when i click on other route, they work, only problem with home, if i click on home, it doesn't bring me to home page
What is the problem?
Note that: When i landed to home page directly http://127.0.0.1:8000/ it is works nice but if i switch to another page and come to home page again by click on home buttion, it is not work

Related

Adding anchor to AngularJS State

I have a state named state.home() that is used to redirect a user to the home pageand this is my state:
$stateProvider
.state('state.home', {
url: '/home',
templateUrl: modulePath + '/views/home/home.html',
controller: 'state:HomeController',
controllerAs: 'homeCtr'
})
In another page after doing some work , in my controller i need to redirect the user to a specific Div in the home page, so in my controller i added this line for the redirection but it doesn't seem to be the right way:
$state.go('state.home',{'#': 'anchor'});
I want to know if it could work with the same state or I have to make a new state.
Give the div an id, e.g <div id="anchor"></div>
in the controller, navigate to the intended state and use anchor scroll to go to the div. Dont forget to inject $anchorScroll
$state.go('state.home').then(function() {$anchorScroll('anchor');})

Angular UI Router redirecting to default page

I have a modular application where i have routes split into multiple files.
So my system is.
Top
--Main.Module.js
--Main.Routes.js
--Main.Controllers.js
--Main.html
--user (folder)
----User.Module.js
----User.Routes.js
----User.Controllers.js
And in the user folder I have a login folder with a Login.html and a register folder with a Register.html
The main module file looks like this
angular.module('Main', ['ionic', 'Main.Routes', 'Main.Controllers', "User"])
.run(function($ionicPlatform) {
$ionicPlatform.ready(function() {
// Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
// for form inputs)
if (window.cordova && window.cordova.plugins.Keyboard) {
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
cordova.plugins.Keyboard.disableScroll(true);
}
if (window.StatusBar) {
// org.apache.cordova.statusbar required
StatusBar.styleDefault();
}
});
});
The main route file contains
angular.module('Main.Routes', [])
.config(function($stateProvider, $urlRouterProvider) {
$stateProvider
.state('Loader', {
url: '/',
templateUrl: '/views/Main.html',
controller: 'MainController'
});
$urlRouterProvider.otherwise('/');
});
And the Main.Module.js injects these two things as well as the User module which js file looks like this
angular.module('User', ['User.Controllers', 'User.Services', 'User.Routes']);
The user routes file has the routes for the login page and the register page
angular.module('User.Routes', [])
.config(function($stateProvider) {
$stateProvider
.state('Login', {
url: '/user/login',
templateUrl: 'views/user/login/Login.html',
controller: 'LoginController'
})
.state('Register', {
url: '/user/register',
templateUrl: 'views/user/register/Register.html'
});
// if none of the above states are matched, use this as the fallback
});
And when I do $state.go("Login") from the main controller it takes me to the login page without any issue. But I have a register button on the login page which is associated with this block of code
//Move user to the register page
$scope.registerClick = function(){
$state.go("Register");
}
Which redirects me to the register page for around half a second then immediately kick it back over to the main html page. So my question is i need to know why the state isnt staying with the register page and is moving immediately to the main.html page. The register page is part of the page history stack because i can go "back" to it with either the hardware back button or pressing back on chrome during testing. I tried moving the routes back to the main route file and not injecting the user routes but it produced the same results.
==============EDIT=============
Once the application loads, if i change the page it goes to after the main page to the register page and add a link back to the login page this behavior does not occur. I'm just confused on whats different.
set an otherwise
.config(function($stateProvider, $urlRouterProvider) {
$urlRouterProvider.otherwise('/defaultpage');
.state('login', {
url: '/login',
templateUrl: 'templates/template.html',
controller: 'Ctrl'
})
.otherstates etc....
});
I see what I did wrong. In the location where I had my button that was supposed to take me to the register page alongside the ng-click directive i also had a href directive set to #. Thank you all for your input though.
The offending line
Create an account
What it should have been
Create an account

Angular js not routing to default page after browser page refresh

I have angularjs route with the following definition:
moduleA.config(function($routeProvider){
$routeProvider.
when('/A',{templateUrl:'A.jsp'}).
when('/B',{templateUrl:'B.jsp'}).
when('/C',{templateUrl:'C.jsp'}).
otherwise({
redirectTo: '/',
templateUrl: 'A.jsp'
});
});
Now, let say I click on something and it is redirected to #/C/ view. After refreshing the page, it is redirecting to view C and not to the default view.
I have to show default page after every page refresh happens.
I thought of changing the url to base url while refreshing the page, so that it can be redirected to default page. I am looking for better alternative for this through Angularjs way.
Thanks in advance.
Try this:
In the app.run() block inject 'window' and $location dependency and add:
window.onbeforeunload = function () {
$location.path('/');
};
Like #maurycy commented, If you want user to go to default page anytime a user he's comming to your application, you don't need the event.
just:
$rootScope.$on('$routeChangeStart', function (event, next, current) {
if (!current) {
$location.path('/');
}
});
in your app.run() function.
It should work

$cookies and hide element on login/logout and ui-router child states

I am stuck at hiding and showing the login and logout buttons at the navbar. I am setting some cookies at LoginCtrl which belongs to login.html. When user logged in I am assigning rootscope to some variables which that variable assing to ngshow/hide in And getting those cookies in HomeCtrl. What i want to achieve is when I click log in button in the login.html Login element at the nav bar has to be gone and Username element has be to show. In plunker when I add some nested states like my local and result is the same. But when i remove the nested structure and add simple two state its start working.
Working Case:
.state('home', {
templateUrl:'home.html', ===>stores the navbar html
controller: 'HomeCtrl'
})
.state('login', {
templateUrl:'login.html',
controller:'LoginCtrl'
})
Not Working Case:
.state('home', {
templateUrl:'home.html, ===>stores the navbar html
controller: 'HomeCtrl'})
.state('home.login', {
templateUrl:'home.login.html',
controller:'LoginCtrl'})
Here is plunker when you logged in login button is in place but when you rerun the app Login button is gone.
http://plnkr.co/edit/tZuvyrAUD0yCN8a3K5lF
You problem was that you put 'some' key but get 'Some'. Here is your example: plnkr.co/edit/ZsT52SYFeRVCXpGYTMqK?p=preview

Page automatically refreshing on AngularJS for some reason

In my app, the page periodically refreshes itself whenever I click a link that loads a different partial via the $routeprovider. Then, in addition to refreshing, it takes me back to the default partial (i.e., I'd have to click the link again in order to get where I wanted to go). I'm not using any methods that refresh the page at all; why might this be happening?
Also, interestingly enough, this doesn't happen when I click any non-$routeprovider links. The following is my routeProvider:
.config(['$routeProvider', '$locationProvider',
function($routeProvider, $locationProvider) {
$routeProvider.
when('/', {
templateUrl: "index.html"
}).
when('/profile/:id', {
templateUrl: "profile.html",
controller: 'UsersController as profiledUser'
}).
when('/gift/:id', {
templateUrl: "gift.html",
controller: 'GiftsController as profiledGift'
}).
when('/users-form', {
templateUrl: "users-form.html",
controller: 'UsersController as newUser'
}).
when('/login-form', {
templateUrl: "login-form.html",
controller: 'UsersController as loginUser'
}).
when('/gifts-form', {
templateUrl: "gift-form.html",
controller: 'GiftsController as newGift'
}).
when('/about', {
templateUrl: "about.html"
}).
otherwise({
redirectTo: '/'
});
Here's an example of one of the links, which links to the currently logged in user's profile, when inspected in Chrome's dev tool:
Profile
Here's that same link in the code, where it's part of a navbar:
<li>Profile</li>
To reiterate, clicking any of the above links occasionally both redirects to .otherwise and refreshes the page. I have not been able to reliably reproduce this behavior, as it seems to happen randomly; strangely enough, it happens even if I remove the .otherwise route from the config.
It looks like you are trying to do 2 things at once with the click on the <a> tag but only 1 is happening:
The default action, which is in the href and says "load UsersController + profile view" and
set template=1 in the current context
Even if template=1 has occurred, once you switch to the UsersController you no longer have access to this setting.
Try something like this:
Profile
plus
.when('/profile/:id/:template', {
controller: 'UsersController as profiledUser',
templateUrl: 'profile.html'
})
Or, use $rootScope or some other global context for the template=1 so it's always available to any controller+view as long as you've dependency injected it.

Categories