我这里有一个简单的代码,但在传递参数时陷入困境。这是代码:
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/