2016-06-29 42 views
0

这个问题是基于this先前的答案。Angular JS - 带有嵌套作用域的http请求时出错

我想送,我想送的要素之一在视图中表示为

{{selectedCountry.shipping[shippingType]}} 

请求是为了使一个单一的请求一起等元素去这样一个HTTP请求我

$scope.checkoutData = angular.extend($scope.selectedCountry.shipping[$scope.shippingType], $scope.selectedCountry.name); 

混合在一起它们,然后用发送请求

$scope.submitForm = function() { 
    $http.post('process.php', $scope.checkoutData) 
}; 

但是当做第二步时,我得到一个错误。您可以看到the working fiddle here(第二步注释,所以它不会破坏代码)。提前致谢!

回答

3

你需要注入$ HTTP到控制器:

app.controller('myCtrl', function($scope, $http) { 
... 
}) 

我在你的plunker看到的错误是:

ReferenceError: $http is not defined 

对于你的第二个问题与angular.extend,请尝试更改提交功能:

$scope.submitForm = function() { 
    var checkoutData = { 
     shippingAmount: $scope.selectedCountry.shipping[$scope.shippingType], 
     country: $scope.selectedCountry.name 
    } 

    $http.post('process.php', checkoutData) 
}; 
+0

这是失踪,我刚刚修复它:)。但仍然然后当我取消注释表达它仍然给出一个错误http://jsfiddle.net/v3o87L5z/ – Joe82

+0

它看起来像$ scope.selectedCountry.shipping [$ scope.shippingType] evalutes到一个数字是30.你不能扩展数字只是对象... –

+1

更新我的答案修补程序 –