AngularJS $http.get with resolve
我开始了解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' }) }); 控制器 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_datau0026amp;table=’$routeParams.table),那就很奇怪了.如果我输入…#/ tables / teszt我没有得到数据,但如果我写完后…#/ tables / teszt2,我得到了teszt表的数据,如果我写了别的东西而不是teszt2那么我得到了teszt2表的数据. 我如何使用网址进行ajax调用? 如果您以不同的方式进行,我将不胜感激.
从
$routeParams docs开始:
所以只需注入$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; }) } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |