AngularJS $ http.get参数

时间:2015-09-30 01:12:43

标签: 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

控制器

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在我的网址中传递ID?谢谢。

1 个答案:

答案 0 :(得分:2)

您的: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 }
        });
    }

}