[英]Angular JS: Passing input parameters in HTTP service (POST)
I'm new to Angular JS and have been stuck in this problem. 我是Angular JS的新手,一直陷在这个问题中。
<body class="homepage" ng-app="angularApp">
<div ng-controller="myCtrl">
<ul>
@foreach ($fruits as $fruit)
<!-- looping -->
<input type="hidden" value="{{$fruit->id}}" ng-model="testParameter">
<button ng-click="testClick(testParameter)" class="adding_item_to_cart">Add to Cart</button>
@endforeach
</ul>
<p ng-bind="testBind"></p>
</div>
<script type="text/javascript">
var angularApp = angular.module('angularApp', []);
angularApp.controller('myCtrl', function($scope, $http) {
$scope.testClick = function(id) {
$http({
method : "POST",
url : "/fruits/add/".id
}).then(function addSucces(response) {
$scope.testBind =response.data;
}, function addError(response) {
$scope.testBind = "Error!";
});
}
});
</script>
</body>
What I've been trying to do is to send a parameter as an input value of HTTP request when the testClick
is invoked to run the POST method, based on which fruit
(ie the contents of the for-loop) has been chosen. 我一直想做的是,在调用
testClick
来运行POST方法时,根据选择的fruit
(即for循环的内容)发送参数作为HTTP请求的输入值。 (In this example, I'm trying to run a Route method of Laravel 5.2). (在此示例中,我尝试运行Laravel 5.2的Route方法)。
So far, I have found some postings on Stackoverflow, which are apparently relevant to this task. 到目前为止,我已经在Stackoverflow上找到了一些与该任务有关的帖子。 The code above is modified several times, following the advice of those postings.
遵循这些发布的建议,对上面的代码进行了多次修改。
I do understand this causes an error, but I'm clueless as to what is actually wrong and what I need to do instead. 我确实知道这会导致错误,但是对于真正的错误和需要执行的操作我一无所知。
ANY advice will be appreciated! 任何建议将被认真考虑!
This: 这个:
url : "/fruits/add/".id
should be this: 应该是这样的:
url : "/fruits/add/" + id
You're saying 'gimme the id property of a string' - that aint gonna work! 您是在说“给一个字符串的id属性”,那样就行了!
In $http
url key there is improper concatenation done, see following fixed code: 在
$http
url键中,连接操作不正确,请参见以下固定代码:
$http({
method : "POST",
url : "/fruits/add/"+ id
}).then(function addSucces(response) {
$scope.testBind =response.data;
}, function addError(response) {
$scope.testBind = "Error!";
});
@YMD - not sure if this is so relevant now, your question has probably been answered by now but since you have not marked any as the right answer, I am responding with this. @YMD-不知道现在是否如此重要,您的问题现在可能已经回答了,但是由于您还没有将任何问题标记为正确的答案,因此我对此做出回应。 Please mark it as correct answer if it answers your question.
如果回答您的问题,请将其标记为正确答案。
The testParameter is undefined in the controller. testParameter在控制器中未定义。 either use $scope from within the controller - $scope.testParameter=" ";
从控制器内部使用$ scope-$ scope.testParameter =“”; or bind it to some object and use object.testParameter .
或将其绑定到某个对象并使用object.testParameter 。
Unless the View passes the variable to the controller, the controller has no way of knowing about it so, testParameter remains just a View scoped variable, with no value assigned to it , and hence, undefined . 除非View将变量传递给控制器,否则控制器将无从得知,因此testParameter仍然只是View范围内的变量, 未分配任何值 ,因此为undefined 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.