2014-01-23 54 views
7

我越来越知道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(现在这两个都可以),一切都可以正常使用,我得到的DATAS,我可以跟他们做任何事情

如果我试试我上面包含的版本$http.get('http://mywebsite.com/doc/ajax/table.php?function=get_table_data&table='+$routeParams.table),这很奇怪。如果我型我...#/tables/teszt没有得到DATAS,但如果我...#/tables/teszt2后写的,我得到的teszt表的数据,如果我写别的东西,而不是teszt2后然后我得到teszt2表的数据。

我怎么能使用URL来使AJAX调用?

我将不胜感激的最佳做法,如果你会用不同的方式做到这一点。

+0

一个缺少'$ scope.apply()'可能? – Jerska

+0

我不知道,因为我不是专家,但我会寻找如何使用它。谢谢小费! – ThePianist

回答