繁体   English   中英

AngularJS $http.get 参数

[英]AngularJS $http.get Parameter

我这里有一个简单的代码,但在传递参数时被卡住了。 这是代码:

路由.js

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

索引.html

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

控制器

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 }
        });
    }

}

暂无
暂无

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

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