What is the best way to pass a Laravel route variable into an AngularJS controller or service?
For example I have a route of domain.com/contacts/87 where 87 is the contact's ID, this route shows the view/edit frontend for the Contact model with ID of 87. I want Angular to somehow get the ID of 87 to make a call to the RestfulAPI to retrieve a JSON representation of the Laravel Contact model, it will also be used for update and delete requests.
Partial from my AngularJS Contact service:
show: function (id) {
var url = '/api/contacts/';
return $http({
url: url+id,
method: "GET"
});
},
I think I have found the answer by passing in the ID using blade/php echo into the ng-init fore example:
<div class="container" ng-controller="contactController" ng-init="init({{$contact->id}})">
contactController partial:
$scope.init = function (contactID) {
if (contactID) {
$scope.contact.id = contactID;
$scope.showContact();
}
...
};
Please let me know if this is a valid/safe way of doing this?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.