javascript - AngularJS $http.get 参数

标签 javascript php angularjs

我这里有一个简单的代码,但在传递参数时陷入困境。这是代码:

route.js

function config($routeProvider) {
    $routeProvider
    .when('/edit/:id', {
        controller: 'UpdateTodoCtrl',
        templateUrl: 'app/views/edit-todo.html'
    });
}

index.html

<div ng-controller="SomeOtherCtrl">
<table>
<tr ng-repeat="t in todos">
<a href="#/edit/{{ t.id }}">{{ t.name }}</a>
</tr>
</table>
</div

Controller

function UpdateTodoCtrl($http, $scope) {

    function init() {
        $scope.todos = null;
        $scope.loading = true;

        $http.get('app/endpoints/edit-todo.php', {
            params: { todoId:  //how to get the id paramter }
        });
    }

}

正如您在 Controller 中看到的那样,我注释掉了我的问题的一部分。如何使用 $http.get 在我的 url 中传递 id?谢谢。

最佳答案

您的 :id 是一个路由参数,因此您可以这样做:

function UpdateTodoCtrl($http, $scope, $routeParams) {

    function init() {
        $scope.todos = null;
        $scope.loading = true;

        $http.get('app/endpoints/edit-todo.php', {
            params: { todoId: $routeParams.id }
        });
    }

}

关于javascript - AngularJS $http.get 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32856219/

相关文章:

javascript - 我试图在 mousedown 之后强制执行 mouseup 事件

javascript - 获取导航栏中的每一行文本并添加为 id

javascript - 如何从服务器获取嵌套路由 ember 2.3.0 的数据

php - 如何在 Laravel 5.2 注册期间为每个新用户将文本写入一个数据库列?

php - 需要在 mysql 中查询多层次营销

php - 您请求了一个不存在的服务 "security.context"

javascript - run 方法中的 Angular $q 服务实现

javascript - AngularJS 中的动画 ng-show

javascript - 如何从 SAPUI5 中的 REST 端点获取模型

javascript - model.save 设置 url 并在成功时回调