AngularJS $http.get with resolve

前端 未结 2 1597
小鲜肉
小鲜肉 2021-01-05 01:01

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,

相关标签:
2条回答
  • 2021-01-05 01:36

    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;
                }
    
    0 讨论(0)
  • 2021-01-05 01:42

    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;
          })
        }
    }
    
    0 讨论(0)
提交回复
热议问题