Having trouble making page change work when set from url - angularjs

I'm building an app using AngularJS and OData Controller. I created the pagination using Angular-ui.
<div class="table" ng-controller="MyCtrl as ctrl">
<!-- bunch of data to be shown here - The table stuff -->
<uib-pagination class="pagination"
total-items="ctrl.totalItems"
items-per-page="10"
max-size="10"
rotate="false"
boundary-links="true"
ng-model="ctrl.currentPage"
ng-change="ctrl.pageChanged(ctrl.currentPage)">
</uib-pagination>
</div>
This works perfect. What happened was when user goes to the next page, select a table for detail view, then click 'Done' or 'Cancel' or whatever button to get out of the detail view to the table view, the view is set back to first page instead of taking them back to whatever page they were before.
So, what I ended up doing, is creating a state using ui-router.
Here's the ui-router state
.state('Claims', {
url: '/Claims?page',
templateUrl: baseUrl + 'Scripts/App/Views/Template/denied/denied-claims-table.html',
params: {
page: {
value: '0',
squash: true
}
}
})
.state('Detail', {
url: '/Claims?page&id/detail',
templateUrl: baseUrl + 'Scripts/App/Views/Template/denied/denied-claims-details-panel.html',
params: {
page: {
value: '0',
squash: true
},
id: {
value: '0',
squash: true
}
}
})
And here's my controller:
deniedClaimsApp.controller('DeniedClaimsController', [
'$scope',
'$state',
'$stateParams',
'DeniedClaimsService',
'SharedDataService',
function ($scope, $state, $stateParams, DeniedClaimsService, SharedDataService) {
var vm = this;
var claimBK = {};
vm.claims = {};
vm.totalItems;
vm.numPages = 10;
vm.currentPage = parseInt($stateParams.page, 10) || 1;
console.log('Current Page: ' + $stateParams.page);
activate();
SharedDataService.registerObserver(activate);
function activate() {
$scope.$emit('LOAD');
vm.isCollapsed = true;
console.log("Page set to : " + vm.currentPage);
//vm.currentPage = DeniedClaimsService.getCurrentPage();
vm.currentPage = $stateParams.page;
vm.claims = DeniedClaimsService.getClaims(vm.currentPage);
angular.copy(vm.claims, claimBK);
resolve();
$state.go('.', { page: vm.currentPage }, { notify: false });
}
function resolve() {
vm.currentPage = $stateParams.page; //adding this didn't help either
console.log("Resolve: " + vm.currentPage);
vm.claims.$promise.then(function (data) {
vm.totalItems = data.count;
$scope.$emit('UNLOAD');
});
}
vm.pageChanged = function (page) {
/*
This is where page is always 1. For some reason, when the page number is changed from the url, it raises the page change event, but the ng-model in uib-pagination doesn't change to the page set in the url. It always stays 1 and changes the page back to the first page
*/
$scope.$emit('LOAD');
//vm.currentPage = $stateParams.page;
console.log("New page: " + vm.currentPage);
console.log('changing page');
vm.claims = DeniedClaimsService.pageChanged(vm.currentPage);
vm.claims.$promise.then(function (data) {
$scope.$emit('UNLOAD');
});
console.log("Changing State to: " + vm.currentPage);
$state.go('.', { page: vm.currentPage }, { notify: false });
console.log("Changing pagination: " + vm.currentPage);
//vm.currentPage = $stateParams.page;
}
So, the controller is only there to change the page, and a couple other things that is not shown here for brevity. When I change the page using the 'Next' or 'Prev' buttons, the page changes fine. But when I change the page from the url and obtain the page from $stateParams.page, it changes the page back to first page no matter which page I was in. I thought it had something to do with the page change event raised when using ng-change. So, I changed uib-pagination ng-change to ng-click="ctrl.pageChanged(ctrl.currentPage). Doing this did change the page when the page number is set in the url as /Claims?page=5then it'd take me to page 5, but it didn't change the ng-model in uib-pagination. That remained 1 even though the table is displaying data for page 5. So, after changing to page 5 from the url, and if I change the page by clicking 'Next', it would take me to page 2 instead of page 6. What am I doing wrong? Why is that model not changing with the change in value for the same model?

Related

AngularJS Include Refresh

I have a html page which I am including as follows.
<ng-include src="lowerTabURL"></ng-include>
This page contains a devextreme control which loads a datasource via ajax.
html:
<div class="tab-container" style="height:100%; width:100%">
<div dx-tabs="lowerTabOptions" dx-item-alias="lowerTab">
</div>
</div>
controller:
DemoApp.controller('NavigationController', function DemoController($scope, $templateCache) {
$scope.lowerTabURL = "LowerPanelTest";
$scope.currentSidebarId = 10100;
$scope.lowerTabOptions = {
dataSource: new DevExpress.data.CustomStore({
load: function (loadTabOptions) {
console.log('get tabs');
var d = $.Deferred();
$.ajax({
url: 'GetLowerTabs',
data: { currentSidebarId: $scope.currentSidebarId },
type: 'GET',
success: function (result) { console.log(result); d.resolve(result); }
});
return d.promise();
}
}),
animationEnabled: true,
swipeEnabled: true,
itemTitleTemplate: 'title',
height: '100%'
};
$scope.navBarClicked = function (sidebarId) {
console.log(sidebarId);
$scope.currentSidebarId = sidebarId;
}
});
This works correctly however I have a navbar which when clicked, should change the tab control.
Currently I am changing the sidebarId which gets passed to the ajax call but I need a way to reload the include page so that this is called again. I have tried changing the lowerTabUrl and then changing it back again but this doesnt refresh the page. What is the best way to do this?
It depends on your angular version, you will need to watch after changes of value for param sidebarId, # angular 1 this is achieved by scope.watch
scope.$watch('sidebarId', function(newValue, oldValue) {
// ..call refresh here
});
at angular 1.5 and later you can override ngOnChages
this.$onChanges = function (changesObj) {
if (changesObj.sidebarId) {
// changesObj.sidebarId.currentValue
}
};

angularjs templates not hiding div from route

It's two page user registeration process depending on the role the second page could be different but the first page will always remain the same. what I want I that user can go forward and backwards on both screens with persistent data. I trying a static page at start and then hide it and add the second template from route.
This is my angular app controller.
app.controller('addlandlordController' , function($scope , $http , $route ,API_URL , $routeParams , uploadService ){
$scope.API_URL = API_URL;
$scope.landVisible = true;
$scope.IsVisible = true;
if( $routeParams.test)
{
scope.$apply(function () {
$scope.IsVisible = false;
});
alert( $routeParams.test);
}
$scope.adduser = function($route){
var data = $.param({
fName: $scope.firstName,
lName: $scope.lastName,
role: 'landlord',
email: $scope.email,
linkId: $scope.linkId,
password: $scope.password,
});
var config = {
headers : {
'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8;'
}
}
$http.post(API_URL + 'user' , data , config)
.then(
function(response){
//$scope.IsVisible = false;
//alert('success');
},
function(response){
// failure callback
alert('fail');
}
);
}
});
I have a div in html like this,.
<div id="content" class="container1" ng-controller='addlandlordController' >
<div ng-show = "IsVisible">
And following is my route in config,.
app.config(function($routeProvider){
$routeProvider.when('/landlord' , {
templateUrl : 'template/addlandlord.html',
controller : 'addlandlordController',
resolve: {
test: function ($route) { $route.current.params.test = true; }
}
})
});
What I want is that when the user click on the following button.
Create an Account</button>
On click that button #/landlord will be added to the url and the route config code will run and add the other template in ng-view which is happening. Now next step is to hide the old div above in such a way that when user go back one sten only the previous code should show and when user goes again into the next screen only the next template should be visible and mean while data should remain same for the both views.
Issues I am facing is
Css is for template view is missing although the css files are already in the commen header. But appears when a place css in the style within template.
if I hide the first div in the response of adduser then if user go back it still hidden. it doesn't appears unless I refresh the page.
But if went to hide it through route config the value turn false but div never hides.
Please check this
scope.$apply(function () {
$scope.IsVisible = false;
});
You are using $apply on scope, but not in $scope.
And $applyAsync is preferable method to trigger digest without risking of error "$digest already in progress"
$applyAsync example:
$element.on('click', ()=>{
$scope.model.testValue = 'I have been updated not from angular circle';
$scope.$applyAsync();
});
Link to the docs
Nice article to read

Dynamically change function of ng-click based on route

I have an app with many views. On most views I have a toggle-able drawer on the left hand side for navigation. However, on a few views I want the menu to be a back button instead.
I am trying to use ng-click and databinding.
md-button ng-click="{{$scope.current.navBarFunction}}"
to dynamically inject the name, from an attribute navBarFunction in my routes, of the function for ng-click However this doesn't work and I'm unsure how to continue.
.when('/articles/:articleId', {
title: 'articles.title',
icon: 'arrow_back',
navBarFunction: 'backButton()',
templateUrl: 'views/articles/:articleid.html',
controller: 'ArticlesArticleidCtrl',
resolve: {
loggedin: checkLoggedin
}
})
Furthermore, is there anyway to make an if statement in app.js using the current route? That would simplify things.
EDIT 1:
here's more code in our controller:
function navBack(pageID) {
$location.path( '/' + pageID );
}
function buildToggler(navID) {
var debounceFn = $mdUtil.debounce(function(){
$mdSidenav(navID)
.toggle()
.then(function () {
$log.debug('toggle ' + navID + ' is done');
});
},300);
return debounceFn;
}
$scope.toggleLeft = buildToggler('left');
$scope.navBackUsers = navBack('users');
$scope.navBackArticles = navBack('articles');
$scope.navBackClassrooms = navBack('classrooms');
Without seeing more of your code it should likely be:
ng-click="current.navBarFunction()"
Not sure why you have () in string value in router or how you are setting this up in directive or controller. Seeing more code would help

$http.get method not working on ng-submit

I want $http.get method to work when a form is submitted.
Here is my code. The object $scope.questions is being set when the method is called but the data doesn't show up in the div. Moreover, when the $http.get method is outside the signIn() function it works just fine.
$scope.signIn = function(data) {
$location.path('/profile');
var url = "database/fetch_data.php?query=";
var query = "Select * from question where userId=2";
url += query;
$http.get(url).success(function(questionData) {
$scope.questions = questionData;
console.log($scope.questions);
});
};
<div>
User Profile
<br/>Question Posted
<br/>
<input ng-model="query.title" id="value" type="text" placeholder="Search by Title..." ">
<div>
<ul>
<li ng-repeat="question in questions | filter: query ">
{{question.title}}
</li>
</ul>
</div>
<br/>
</div>
You need to move your $location.path('/profile') inside your http request. Remember that a http request is async call. You should redirect after getting the data not before.
$scope.signIn = function(data) {
var url = "database/fetch_data.php?query=";
var query = "Select * from question where userId=2";
url += query;
$http.get(url).success(function(questionData) {
$scope.questions = questionData;
console.log($scope.questions);
$location.path('/profile');
});
};
If you're redirecting to another route with a completely separate scope you will lose any scope you're setting in the success handling.
From what I'm reading you're clicking a button to do an action. After that action you're redirecting to another page with a separate controller and trying to persist the data.
Unfortunately, Angular hasn't figured out a great way to do this. The easiest way to persist data through controllers and scope is to create a service that will store it in one controller and grab it in another controller.
For instance:
$scope.signIn = function(data) {
var url = "database/fetch_data.php?query=";
var query = "Select * from question where userId=2";
url += query;
$http.get(url).success(function(questionData) {
$location.path('/profile');
storageService.store("question", questiondata)
});
};
Your new factory to persist data through:
angular.module('moduleName').factory('storageService', [
function () {
return {
store: function (key, value) {
localStorage.setItem(key, JSON.stringify(value));
},
get: function(key) {
return JSON.parse(localStorage.getItem(key));
},
remove: function(key) {
localStorage.removeItem(key);
}
}
}
]);
Other controller to access data:
$scope.question = storageService.get("question");
// remove localstorage after you've grabbed it in the new controller
storageService.remove("question");
An alternative to doing the somewhat 'hacky' way of using localStorage to persist data through controllers is to use ui-router and have a resolve on the route you're redirecting to.
For instance:
$scope.signIn = function(data) {
$state.go('profile');
};
In your route file:
.state('profile', {
url: '/profile'
controller: profileControllerName,
templateUrl: 'profileHtmlTemplate.html',
resolve: {
'questions': [function() {
var url = "database/fetch_data.php?query=";
var query = "Select * from question where userId=2";
url += query;
$http.get(url).success(function(res) {
return res.data;
});
}]
}
}
In your profile controller:
Inject your 'questions' resolve into your controller and assign `$scope.question = questions;
This will make the HTTP call as soon as you click the route, return the data if successful, then render the page. It will NOT render the page if the resolve does not return success. This will ensure your data will be loaded before you load the page that depends on that data.
I would highly recommend using services to hold your HTTP calls for specific parts of your application. If you have a GET questions, POST question, PUT question. I would create a questionService and make all my HTTP methods there so you don't have to clutter your routes. You would only have to call:
.state('profile', {
url: '/profile'
controller: profileControllerName,
templateUrl: 'profileHtmlTemplate.html',
resolve: {
'questions': [function() {
return questionService.getQuestions(id).then(function(res) {
return res.data;
})
}]
}
}

$location.path causes a full page refresh

I have a page with a link. If the user clicks on the link a popup will display, using ng-show. This is the ng-click:
<div class="adItem" ng-click="showArticleMethod(tiki._id, tiki.tikiTitle)" ng-repeat="tiki in tikis track by tiki._id">
On click a showArticleMethod gets called in the controller:
$scope.showArticleMethod = function(tikiID, article){
var title = $filter("underscoreString")(article)
$location.path(tikiID + "/" + title)
$scope.showArticle = tikiID
}
The method also changes the url to: index.html#/1/title using $location.url().
Since i want people to be able to directly link to the article i have set up this route:
$routProvider.when("/", {
templateUrl: "partials/promise.html",
controller:"tiki.controllers.promise",
reloadOnSearch:false
}).when("/:id/:title/", {
templateUrl:"partials/promise.html",
controller:"tiki.controllers.promise",
reloadOnSearch:false
}
Everything works fine except the animation (opacity:0 to opacity:1) that is set on the popup does not work.
My guess is that the url is not just changed but also reloaded so i tried adding 'reloadOnSearch:false' but that doesn't have any effect.
How can i change the url without reloading the url and keeping the css animation?
This is my full controller:
/* Controllers */
angular.module('tiki.controllers').controller("tiki.controllers.promise", ["$scope", "tikiDB", "$routeParams", "$filter", "$location", function($scope, tikiDB, $routeParams, $filter, $location){
$scope.tikis = tikiDB.query()
$scope.location = ""
$scope.showArticleMethod = function(tikiID, article){
var title = $filter("underscoreString")(article)
$location.path(tikiID + "/" + title)
$scope.showArticle = tikiID
}
$scope.hideArticleMethod = function(){
$location.path("")
$scope.showArticle = false
}
if($routeParams){
$scope.showArticle = parseInt($routeParams.id);
}
}])
The variable 'showArticle' is set to 'false' if no article is shown. It is set to the article ID if an article has to be shown. The article is shown in a popup that is activated using a class that is added to the <body> tag.
<body ng-class="{'bodyShowArticle': showArticle}" ng-view="">
If i add a $timeout then the animation works again but does a full page refresh after the timeout:
$timeout(function(){
$location.path(tikiID + "/" + title)
}, 1000)

Resources