我开始了解AngularJS,我有一个有趣的问题.我开始了解routeProvider,我想我可以编写我的应用程序,就像你搜索一个表名一样,它会改变路由,所以你也可以在url之后写表.
来自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'
})
});
Run Code Online (Sandbox Code Playgroud)
控制器
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;
});
Run Code Online (Sandbox Code Playgroud)
基本上这就是你所需要的,但如果你愿意,我可以包含更多代码.
当我在$ http.get中使用 ...function=get_table_data&table=teszt或...function=get_table_data&table=teszt2(现在这两个可用)一切正常,我得到数据,我可以用它们做任何事情
但是,如果我尝试上面包含的版本$http.get('http://mywebsite.com/doc/ajax/table.php?function=get_table_data&table='+$routeParams.table),那就很奇怪了.如果我输入...#/tables/teszt我没有得到数据,但如果我写后...#/tables/teszt2,我得到teszt表的数据,如果我之后写了别的东西而不是teszt2然后我得到teszt2表的数据.
我如何使用网址进行ajax调用?
如果您以不同的方式进行,我将不胜感激.
Ila*_*mer 12
请注意,$ routeParams仅在路由更改成功完成后更新.这意味着您不能依赖路由解析函数中的$ routeParams正确.相反,您可以使用$ route.current.params来访问新路由的参数.
所以只需注入$route而不是$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;
})
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11523 次 |
| 最近记录: |