加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

AngularJS $http.get with resolve

发布时间:2020-12-17 07:18:03 所属栏目:安全 来源:网络整理
导读:我开始了解AngularJS,我有一个有趣的问题.我开始了解routeProvider,我想我可以编写我的应用程序,就像你搜索一个表名一样,它会改变路由,所以你也可以在url之后写表. 来自app.js的详细信息 app.config(function($routeProvider){ $routeProvider .when('/',{ t
我开始了解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开始:

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.

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读