D
D
Demid Borodin2014-12-14 01:16:39
Angular
Demid Borodin, 2014-12-14 01:16:39

Angular + Laravel how to get id from browser string?

I am writing a small Laravel + Angular application.
Application pages are handled by Angular, Laravel provides "API"
There is a controller that displays a record from the database by id

<?php

class ShowOneWorkpeopleController extends BaseController {
    public function showOneWorkpeople($id) {
        $id = (int)$id;

        $workpeople = Workpeoples::showOneWorkpeople($id)->toJson();

        return $workpeople;
    }
}

The controller is assigned a Route that takes the entry id
Route::get('api/ShowWorkpeople/{id}', '[email protected]');

And now to Angular itself.
There is a controller
crmApp.controller('CrmWorkPeopleShowController', ['$scope', function($scope,$http){
    $scope.watispage = 'Сотрудник';

    $http.get('api/ShowWorkpeople/???').then(
        function(result){
            $scope.workpeople = result.data;
        }
    )
}]);

UPD1 forgot to mention Angular paths
crmApp.config(['$routeProvider', '$locationProvider',
    function ($routeProvider, $locationProvider) {
        $locationProvider.html5Mode(true);

        $routeProvider
            .when('/', {
                controller: 'CrmIndexController',
                templateUrl: 'templates/crmIndex.html'
            })
            .when('/workpeople', {
                controller: 'CrmWorkPeopleIndexController',
                templateUrl: 'templates/workpeopleIndex.tpl.html'
            })
            .when('/workpeople/show/:id', {
                controller: 'CrmWorkPeopleShowController',
                templateUrl: './templates/workpeopleShow.html'
            })
    }]);

So, the question is, what should be put instead of "???", so that the id from the browser line gets there?
The ID must be taken from the last Route. I need only one digit in the output, not the whole path.
With $location, I get the whole path, is there any more elegant solution than iterate over the string and take the last value after the "/"?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
Terminaft, 2014-12-14
@demidborodin

are you using ngRoute?

crmApp.controller('CrmWorkPeopleShowController', ['$scope', '$http', '$routeParams', function($scope, $http, $routeParams){
    $scope.watispage = 'Сотрудник';

    $http.get('api/ShowWorkpeople/' + $routeParams.id).then(
        function(result){
            $scope.workpeople = result.data;
        }
    )
}]);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question