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.