1

I have Two pages in my website. One is EventList and other is EventDetail. When I click on any of the Event in EventList Page then it redirect me to EventDetail Page. I took EventId of Event to fetch the details and populate the corresponding data in EventDetail Page. This happens fine when I do it for first time. When I refresh the page then EventId of Event wiped away. I am using ui-router for passing the EventId as State Parameter to EventDetail Page.

StateRouter.JS

app.config(function ($stateProvider, $urlRouterProvider) {
    $stateProvider
        .state('home.eventlist', {
            url: '/eventlist',
            views: {
                'homedetailview':
                {
                    templateUrl: 'EventsPages/ListEvent.html',
                    controller: 'ctrlEventList'
                }
            }
        })
        .state('home.eventlist.eventdetail', {
            url: '/eventdetail',
            params: { paramOne: "defaultValueOne" },
            views: {
                'homedetailview@home':
                {
                    templateUrl: 'EventsPages/AddEvent.html',
                    controller: 'ctrlEventContent',

                }
            }
        })
})

EventList.HTML

<form class="form-main">
    <div class="div-main">
        <div class="gridBigStyle" ui-grid="gridEventList" ui-grid-pagination>
        </div>
    </div>
</form>

EventListController.JS

myApp.controller("ctrlEventList", ['$scope', function ($scope) {
    $scope.gridEventList = {
        data: 'eventlistdata',        
        columnDefs: [
            { field: 'SiteEventNumber', displayName: 'Event Number' },           
            { field: 'EventName', displayName: 'Event Name' },            
            { name: 'actions', displayName: 'Actions', cellTemplate: $scope.actions}
        ]
    }

    $scope.actions =
        '<a ui-sref=".eventdetail({paramOne:row.entity.EventId})"></a>';                  
}])

EventDetailController.JS

myApp.controller("ctrlEventContent", ['$scope', '$stateParams', function ($scope, $stateParams) {
    alert($stateParams.paramOne);
}])

When I refresh the page then it is showing alert as "defaultValueOne" which is mentioned as default value of StateParams.

I searched net and see that local storage can solve this problem but I dont know how to implement here and what is the disadvantage over other options. It would be great if working example is provided on this.

simple user
  • 349
  • 3
  • 22
  • 44
  • Try like url: '/eventdetail?paramOne' in your router – Sudhir Ojha Jun 01 '18 at 05:04
  • I don't want to make my param value visible to end user by exposing in url. – simple user Jun 02 '18 at 08:17
  • [https://stackoverflow.com/questions/39128931/clear-localstorage-on-tab-browser-close-but-not-on-refresh?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa](https://stackoverflow.com/questions/39128931/clear-localstorage-on-tab-browser-close-but-not-on-refresh?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa) Try this. – Bishnu Kumar Bhakat Jun 04 '18 at 06:10

1 Answers1

0

If you are getting some values from Params, So you can store that data in localstorage or as $rootscope (any of them will may help you to keep data after refreshing.) Actually $StateParams use to send data from one to another so it keeps data one at a time while you are sending params that's all... Hope you are got your points Thanks.

  • Localstorage is better but I am not able to understand one thing as how will it behave in following case: Suppose I have controller page where I set localstorage variable with $stateParams value. Now when I open the page in one tab from A then localstorage will have X value store and if I open the page in another tab from B then localstorage will have value Y stored. If I refresh first tab then I think it will retrieve Y as it is last stored and show details of Y in first tab. Is my concern correct? How to overcome this scenario? – simple user Jun 02 '18 at 08:22
  • Hi! If you are not able to understand, so you can try it and try to debug your code at live and change your tabs and see what will be happening with the $localStorage service in angularJS and you can see that is value of localStorage changed or not... Ex: $localStorage.currentUser = "Hi" after changes tabs you can see value of currentuser will remain unchanged.It will be changed untill unless,if you want to do...\ – Bishnu Kumar Bhakat Jun 02 '18 at 10:50
  • I got your comment. As localstorage will be saved in the disk forever then I need to clear this out during logout. This is OK but what will happen if someone closed the browser or closed the tab. How to remove the localstorage in such cases. Can you please help? – simple user Jun 04 '18 at 03:33