8

I'm getting to know AngularJS and I have an interesting problem. I'm getting to know the routeProvider and I thought I could write my app like if you search a table name, it'll change the route, so you can write the table right after the url, too.

Details from app.js

app.config(function($routeProvider){
    $routeProvider
        .when('/', {
            templateUrl: 'pages/index.html',
            controller: 'homeCtrl'
        })

        .when('/tables/:table', {
            templateUrl: 'pages/about.html',
            controller: 'aboutCtrl',
            resolve: {
                tables: function($http, $routeParams){
                    return $http.get('http://mywebsite.com/doc/ajax/table.php?function=get_table_data&table='+$routeParams.table)
                                .then(function(response){
                                    console.log($routeParams.table);
                                    return response.data;
                                })
                }
            }
        })

        .otherwise({
            templateUrl: 'pages/404.html'
        })
});

The controller

angular
    .module('testApp')
    .controller('aboutCtrl',function($scope, tables){

        $scope.title = "This is the about page!";

        // declare helper variables to not use
        // $scope inside a loop
        var rawFields = [];
        var titleNames = [];

        // load the thead titles (keys)
        angular.forEach(tables[1], function(value, key){
            titleNames.push(key);
        });

        // load table datas without the first object that
        // contains other informations
        for (var i=1; i<tables.length;i++) {
            rawFields.push(tables[i]);
        };

        // set $scope variables to use them in the HTML
        $scope.fields = rawFields;
        $scope.tableName = tables[0].TableName;
        $scope.titles = titleNames;
    });

Basically that's all you need, but I can include more code if you want.

When I use in the $http.get ...function=get_table_data&table=teszt or ...function=get_table_data&table=teszt2 (now these two are available) everything is working perfectly, I get the datas and I can do anything with them

BUT if I try the version I included above $http.get('http://mywebsite.com/doc/ajax/table.php?function=get_table_data&table='+$routeParams.table), that's working strange. If I type ...#/tables/teszt I don't get the datas, but if I write after ...#/tables/teszt2, I get the teszt table's data and if I write something else after instead of teszt2 then I get the teszt2 table's data.

How could I use urls to make ajax calls?

I would appreciate any best practices if you would do it in a different way.

2
  • A missing $scope.apply() maybe ? Commented Jan 23, 2014 at 13:45
  • I don't know, as I'm not an expert yet I'm going to look for how to use it. Thanks the tip! Commented Jan 23, 2014 at 13:56

2 Answers 2

12

From $routeParams docs:

Note that the $routeParams are only updated after a route change completes successfully. This means that you cannot rely on $routeParams being correct in route resolve functions. Instead you can use $route.current.params to access the new route's parameters.

So just inject $route instead of $routeParams:

resolve: {
    tables: function($http, $route){
      return $http.get('http://mywebsite.com/doc/ajax/table.php?function=get_table_data&table='+$route.current.params.table)
      .then(function(response){
        return response.data;
      })
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

try this:

tables: function($http, $routeParams, $rootScope){
                var promise = $http.get('http://mywebsite.com/doc/ajax/table.php?function=get_table_data&table='+$routeParams.table)
                            .then(function(response){
                                console.log($routeParams.table);
                                return response.data;
                            });
                if($rootScope.$$phase != '$apply' && $rootScope.$$phase != '$digest') {
                                    $rootScope.$apply();
                }
                return promise;
            }

1 Comment

:/ Same issue occurs. Only binds the data after changing the url.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.