i'am trying to localize the baSidebar Navigation of Akveos BlurAdmin.
In the ba-sidebar.html i'am using the translate-filter like {{ ::item.title | translate }}
and the state ist set like:
function routeConfig($stateProvider, dashboardProvider) {
$stateProvider
.state('stateName', {
url: '/stateName',
templateUrl: 'app/pages/[...],
controller: 'stateNameCtrl',
title: 'TITLES.STATENAME',
sidebarMeta: {
order: 200,
},
}) [...]
Angular gets the translation of TITLES.STATENAME from a .json-file. The title of the contentTop is perfectly changing by changing the active language. (i'm using $state.reload(); by ng-click). But the sidebar wont reload. The title still be in the same language which was active while loading the page. If i log off and log in again, the new language is active and shown correctly.
Is there a way to reload the ba-sidebar.html within an ng-click-event like i did with $state.reload()? (i dont want to reload the entire page, because then the site reloads all defaults including the default language)
I solved the problem by saving the language in cookies and reloading the entire page. on load it gets the language from the cookies. if this languagekey isn't set, it loads the default language.
For people who get stuck in the same issue: here is a documentation of using cookies in javascript: http://www.w3schools.com/js/js_cookies.asp
I'm totally new to Ionic and JavaScript and I'm having a few problems when changing views in my project.
I've created the project using the tabs starter, and everything works when moving withing the default tabs and even when adding new ones. At certain parts of the code, I need to move to a tab view so I'm using the $state.go() function and it works fine. However, I've added a new view with its template, controller, and state and I need to load it when clicking a button. So I use $state.go() again, but the URL in the explorer changes but not the view, it stays in the previous one. It doesn't work either when I type the URL in the browser, so maybe the problem is in the state definition, but I'm pretty sure it is ok.
State
.state('tab.file_explorer', {
url: '/file_explorer',
views: {
'tab-file_explorer': {
templateUrl: 'templates/tab-file_explorer.html',
controller: 'file_explorerCtrl'
}
}
})
Controller
It is in the controller of another view where the button is located (the button works fine, I've tested it). The file_explorerCtrl controller is empty at the moment.
.controller('load_csvCtrl', function($scope, $state) {
$scope.loadCsv = function(){
$state.go('tab.file_explorer');
}
}
This redirects to the URL http://localhost:8100/#/tab/file_explorer, with no console output or 404 errors. I've also tried using $location.path() and $window.location.assign() but it also fails to load the template.
I'd appreciate any help.
Can you check if your index.html has <ion-nav-view></ion-nav-view> tag
You're using named views. your <ion-nav-view> should match the name:
<ion-nav-view name="tab-file_explorer"></ion-nav-view>
Check the docs here:
https://github.com/angular-ui/ui-router/wiki/Multiple-Named-Views
Set cache-view="false" in View.
Example:
<ion-view cache-view="false">
<!-- View Content goes here -->
</ion-view>
I have a PhoneGap app, that has multiple html pages.
I use one controller, called AppController, that loads the data for the startup screen and default pages.
I added a new page to the navigation bar, which, when clicked & opened should load up data from the server with a php call, so it should only make a php call, when the page is shown.
This page uses the same controller as the rest of the app.
I am really new to AngularJS, so I might've programmed it in a bad way, but the rest of the data for the home page is loaded in the appController like this
myApp.controller('AppController', function ($scope, $timeout, sharedProperties) {
$scope.items= {};
$scope.items[item1] = {....} //LOADING UP the items collection
}
How can I hook up an "onload" event or something, that would only fire when the page is shown?
There are so many ways you could do that, one of them:
// inside your controller
angular.element(document).ready(function () {
// your code
});
another one:
<div ng-controller="myCtrl" ng-init="someinitfunc()"></div>
How about using ng-init() directive on your next page ?
Using the following code, when a page with id=0 loads first time there is no problem with controller. But when again the same page loads with same id=0 again, it does not loads controller.
$state.go('tab.dash', {
id: $rootScope.products[CONSTANTS.i].id
}, {
reload: true
});
How does it happen? Please suggest me a solution.
I encountered a similar problem where I needed stats to recalculate every time a tab was visited.
You need to disable view caching. You can do so in the route setup. For example:
.state('tab.stats', {
url: '/stats',
views: {
'tab-stats': {
templateUrl: 'templates/tab-stats.html',
controller: 'StatsCtrl'
}
},
cache: false
})
well, When you cache the view (by default it is true) controller is loaded only at first time and on subsequent navigation it will attach and detach the scope. Cacheing helping with the performance of single page applications. If you dont want to disable the caching then you use the ionic view events like (enter, leave,loaded and so on).
$scope.$on('$ionicView.enter', function () {
// ur stuff in here....
});
I use routeProvider to define controlers and templates for my urls.
When I click on the link, which has the same url as is the actual location, nothing happens. I would like the reload() method to be called if a user clicks on such a link even if the location hasn't changed. In other words, if I set the location to the same value, I would like it to behave the same as if I would set it to different value.
Is there a way to configure routeProvider or locationProvider to do it automatically? Or what is the right approach to do this? This is stadard behaviour in round trip applications, but how to do it in angularjs?
I've asked it on google groups as well.
UPDATE:
This question is getting lots of views, so I will try to explain how I solved my problem.
I created a custom directive for linking in my app as Renan Tomal Fernandes suggested in comments.
angular.module('core.directives').directive('diHref', ['$location', '$route',
function($location, $route) {
return function(scope, element, attrs) {
scope.$watch('diHref', function() {
if(attrs.diHref) {
element.attr('href', attrs.diHref);
element.bind('click', function(event) {
scope.$apply(function(){
if($location.path() == attrs.diHref) $route.reload();
});
});
}
});
}
}]);
The directive is then used for all links in my app I want to have this functionality.
<a di-href="/home/">Home</a>
What this directive does is that it sets the href attribute for you based on di-href attribute so angular can handle it like always and you can see the url when you hover over the link. Furthermore when user clicks on it and the link's path is the same as the current path it reloads the route.
Add a / (slash) to the defined url in the route configuration
I met a similar problem today, I have a link in my web page and when I click it, I want the ng-view reload each time, so that I can refresh data from server. But if the url location doesn't change, angular doesn't reload the ng-view.
Finally, i found a solution to this problem. In my web page, I set the link href to:
test
But in the route config, I set:
$routeProvider.when('/test/', {
controller: MyController,
templateUrl:'/static/test.html'
});
The different is the last slash in url. When I click href="#/test" for the first time, angular redirect the url to #/test/, and load ng-view. when i click it second time, because the current url is #/test/, it's not equal to the url in the link (href="#/test") I clicked, so Angular triggers the location change method and reloads the ng-view, in addition Angular redirects the url to #/test/ again. next time i click the url, angular does the same thing again. Which is exactly what I wanted.
Hope this was useful for you.
You can add a _target='_self' on the link to forces the page to reload.
e.g.
{{customer.Name}}
Tested with version 1.0.5 and 1.2.15 on IE and Firefox.
Here's more information from AngularJS site :
Html link rewriting
When you use HTML5 history API mode, you will need different links in different browsers, but all you have to do is specify regular URL links, such as:
link
When a user clicks on this link,
In a legacy browser, the URL changes to /index.html#!/some?foo=bar
In a modern browser, the URL changes to /some?foo=bar
In cases like the following, links are not rewritten; instead, the browser will perform a full page reload to the original link.
Links that contain target element
Example: link
Absolute links that go to a different domain
Example: link
Links starting with '/' that lead to a different base path when base is defined
Example: link
you should use $route.reload() to force the reload.
I don't know if is there a 'automatic' way to do this, but you can use ng-click on these links
For people who are using AngularUI Router. You can use something like this:
<a data-ui-sref="some.state" data-ui-sref-opts="{reload: true}">State</a>
Notice the reload option.
Found the answer here: https://stackoverflow.com/a/29384813/426840
From #Renan Tomal Fernandes answer. following is an example
HTML
<a href="#/something" my-refresh></a>
JS
angular.module("myModule",[]).
directive('myRefresh',function($location,$route){
return function(scope, element, attrs) {
element.bind('click',function(){
if(element[0] && element[0].href && element[0].href === $location.absUrl()){
$route.reload();
}
});
}
});
I think it's a simpler approach.
.directive ('a', function ($route, $location) {
var d = {};
d.restrict = 'E';
d.link = function (scope, elem, attrs) {
// has target
if ('target' in attrs) return;
// doesn't have href
if (!('href' in attrs)) return;
// href is not the current path
var href = elem [0].href;
elem.bind ('click', function () {
if (href !== $location.absUrl ()) return;
$route.reload ();
});
};
return d;
});
Assuming You want to make all basic <a> links (without target attribute) reload on click and You use relative links in the href attribute (e.g. /home instead of http://example.com/home) You don't have to add any special markup to your HTML (comes handy when updating a site with HTML already written).
In my case if the url is same, nothing worked including $route.reload(), $location.path(), $state.transitonTo() etc.
So my approach was Using Dummy Page as follows,
if( oldLocation === newLocation ) {
// nothing worked ------------
// window.location.reload(); it refresh the whole page
// $route.reload();
// $state.go($state.$current, null, { reload: true });
// $state.transitionTo($state.current, $stateParams, {reload:true, inherit: false, notify: false } );
// except this one
$location.path('/dummy');
$location.path($location.path());
$scope.$apply();
}
You need to make '/dummy' module somewhere, the module doesn't do anything, it only change url so that next $location.path() can be
applied. Don't miss $scope.$apply()
I ran into this issue a moment ago, except for it was the home page '/'. I wanted a simple solution with less code. I just took advantage of the .otherwise method in the $routProvider
So in the html link looks like:
Home
since there is no '/home' page specified in the routProvider it will redirect to '/' via the 'otherwise' method. page with this set up:
.otherwise({
redirectTo: '/'
});
Hope it helps someone
I tried Wittaya's solution above using directive approach. Somehow the directive keeps throwing error. I end up with this solution
HTML
Devices
Controller
$scope.stateGo = function (stateName) {
if ($state.$current.name === stateName) {
$state.reload();
} else {
$state.go(stateName);
}
}
Just tried adding this
$(window).on('popstate', function(event) {
//refresh server data
});
and it works fine