gpt4 book ai didi

AngularJS $http.get 解析

转载 作者:行者123 更新时间:2023-12-04 06:19:16 27 4
gpt4 key购买 nike

我开始了解 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'
})
});

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;
});

基本上这就是您所需要的,但如果您愿意,我可以包含更多代码。

当我在 中使用时$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表的数据。

如何使用 url 进行 ajax 调用?

如果您以不同的方式进行操作,我将不胜感激。

最佳答案

来自 $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.



所以只需注入(inject) $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;
})
}
}

关于AngularJS $http.get 解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21309661/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com