简体   繁体   English

将Laravel路由变量传递到AngularJS控制器中

[英]Pass Laravel Route Variable into AngularJS Controller

What is the best way to pass a Laravel route variable into an AngularJS controller or service? 将Laravel路由变量传递到AngularJS控制器或服务中的最佳方法是什么?

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. 例如,我有一个domain.com/contacts/87路由,其中​​87是联系人的ID,此路由显示ID为87的Contact模型的视图/编辑前端。我希望Angular以某种方式获取ID 87调用RestfulAPI以检索Laravel Contact模型的JSON表示形式,它还将用于更新和删除请求。

Partial from my AngularJS Contact service: 来自我的AngularJS联系服务的部分信息:

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: 我想我已经通过使用Blade / php echo将ID传递到ng-init的例子中找到了答案:

<div class="container" ng-controller="contactController" ng-init="init({{$contact->id}})">

contactController partial: contactController部分:

$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? 请告诉我这是否是有效/安全的方法?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM