AngularJS $http.get with resolve

前端 未结 2 1609
小鲜肉
小鲜肉 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: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;
          })
        }
    }
    

提交回复
热议问题