Sunday, 2 April 2017

angular 1 - routing and api

Trying to write a simple SPA. Using MEAN.

API through node are all working.

Have set up routes in my main controller

    // 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"


});

the root route work finds calls the API generates a table. All good

then main part of that template is this

  <tbody id="tenantsTable">
                      <tr ng:repeat = "tenant in details | filter :  true: tenant.activeTenant ">
                        <td><a ng-href = "#/tenant.html?id=" >View</td>
                        <td> </td>
                        <td> </td>
                        <td> </td>
                        <td> </td>
                        <td>$ </td>
                        <td>  </td>
                       </tr>   

                      </tbody>

I want to click on the "View" link and pull data from DB for each tenant through and API call that just needs the Tenant id - standard stuff.

My controller is not picking up the request and when i click on the link on the Tennants page it just ends up blank.

what am i missing here? the api call s are all good and working fine



via Scott S

No comments:

Post a Comment