2017-04-02 46 views
1

試圖寫一個簡單的SPA。使用MEAN。角1 - 路由和api

API通過節點都工作。

在我的主控制器

// create the module and name it 
var boardingApp = angular.module('boardingApp', ['ngRoute']); 


boardingApp.config(function($routeProvider) { 


    $routeProvider 

     // route for the home page that lists all tenants 
     .when('/', { 
      templateUrl : 'js/templates/tenants.html', 
      controller : 'tenantsController' 
     }) 
     // route for a specifc tenant 
     .when('/tenant/:id', { 
      templateUrl : 'js/templates/tenant.html', 
      controller : 'tenantController' 
     }) 

}); 

boardingApp.controller('tenantsController', ['$scope','$http','$log', function($scope, $http,$log) { 

     $http.get(URL + "/api/tenants") 
      .then(function(response){ $scope.details = response.data; }); 

}]); 

boardingApp.controller('tenantController', ['$scope','$http','$location','$log', function($scope, $http, $location, $log) { 



     if ($location.search().hasOwnProperty('id')) { 
      var myid = $location.search()['id']; 
      console.log(myid) 

      myURL = URL + "/api/tenants/"+myid 
      console.log(myURL) 

      $http.get(myURL) 
      .then(function(response){ $scope.tenant = response.data; }); 
      console.log($scope.tenant) 
     } 

}]); 

boardingApp.controller('readmeController', function($scope, $http) { 

     $scope.message = "This is the message" 


}); 

根本工作路線發現調用API生成一個表設置路線。該模板的所有好

然後主要部分是

<tbody id="tenantsTable"> 
         <tr ng:repeat = "tenant in details | filter : true: tenant.activeTenant "> 
         <td><a ng-href = "#/tenant.html?id={{tenant._id}}" >View</td> 
         <td>{{tenant.roomNumber}} </td> 
         <td>{{tenant.firstName}} </td> 
         <td>{{tenant.lastName}} </td> 
         <td>{{tenant.paymentFrequency}} </td> 
         <td>${{tenant.rentAmount}} </td> 
         <td> {{tenant.leaseEnd | date: 'dd MMM yyyy'}} </td> 
         </tr> 

         </tbody> 

我想點擊「查看」鏈接,通過從數據庫中提取數據,爲每個租戶和API調用,只需租戶ID - 標準的東西。

我的控制器沒有收到請求,當我點擊Tennants頁面上的鏈接時,它只是空白。

我在這裏失蹤了什麼? API調用s爲所有的好,做工精細

回答

0

您錨標籤不正確,應該是在正確的格式,如:"#/tenant/{{tenant._id}}"

ng-href="#/tenant/{{tenant._id}}" 

此外,你應該使用$routeParams.id API,而不是獲取內部tenantController路由參數$location.search()這將尋找查詢參數。

+0

謝謝..長時間盯着這個。將需要研究$ routeParams,錯過了 –

+0

@ScottS只是在你的控制器中注入'$ routeParams'並執行'$ routerParams.id'來檢索參數值' –