0

Having problem with my angular app, when I refresh page it doesn't show me correct page. It looks like the way I pass parameter to the page is not right.

So I have my state in the ui-router:

.state('stateDashboard', {
                    url: '/dashboard',
                    templateUrl: '/templates/dashboard.html',
                    controller: 'dashboardController'
        })

In the page where I have list of Dashboards:

<div class="tile" ng-repeat="d in dashList" ng-class="[d.tileColour, d.tileSize]" ui-sref="stateDashboard" ng-click="setDashboard(d.id)">
    <div class="tile-body">
        <i class="fa fa-dashboard"></i>
    </div>
    <div class="tile-object">
        <div class="name">
            {{d.name}}
        </div>
    </div>
</div>

where setDashboard(id) sett id to $rootScope.dashId

and in my controller to display dashboard I have:

Dashboards.getDashboard({ id: $rootScope.dashId }).$promise.then(function (result) {
        $rootScope.model = result;
     }

So now when I refresh the page $rootScope.dashId is undefined so doesn't display page correctly. How can I fix it?

5
  • where are you getting this dashId from? Commented Dec 2, 2015 at 11:32
  • from the dashList, please look on the second code snippet Commented Dec 2, 2015 at 11:45
  • Your code doesn't quite match up here. The HTML is showing dashList, which would be $scope.dashList. the JavaScript you posted is referring to $rootScope.model. Not only are you not showing how you get from $rootScope to $scope, but using $rootScope at all is an anti-pattern. Commented Dec 2, 2015 at 11:45
  • Sorry I only posted part of the code to simplify it. The html have ng-click="setDashboard(d.id)", which assign id to $rootScope.dashId. As I believe this is incorrect way of passing parameter from 1 controller to the other. That is why I asking for help here. Commented Dec 2, 2015 at 11:52
  • You have to pass the dashId as parameter. setDashboard is set On Click. When you refresh or load the new page the rootScope is recreated. Commented Dec 2, 2015 at 12:30

1 Answer 1

1

Add your dashId to the URL so you can pass it when changing dashboard and/or refreshing the page. So avoid calling setDashboard because it will only have effect until you reload the page.

.state('stateDashboard', {
                    url: '/dashboard/:dashId',
                    templateUrl: '/templates/dashboard.html',
                    controller: 'dashboardController'
        })

Now inside your controller you can get it like this. No need to use the $rootScope.

Dashboards.getDashboard({ id: $stateParams.dashId }).$promise.then(function (result) {
        $rootScope.model = result;
     }
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for the answer. I tried that but when I press refresh or just go localhost:28422/dashboard/29, I receive lots of "Uncaught SyntaxError: Unexpected token <" for all my js files
That does not seem related. It is a Syntax error, You might have a typo somewhere.
Well the page works correctly when I direct to it from other page. But only when refresh or go localhost:28422/dashboard/29 it appear with error
to be even more weird, when I open any of the js file in the Chrome Sources I see html of my page and not the js file
That is a weird behavior. Did you check that your backend is properly routing '/dashboard/:dashId', to Angular?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.