AngularJS ng repeat:将对象推送到数组
我有一个selectedProduct的对象。现在,我想创建这些selectedProduct对象的数组,以便使用ng repeat构建列表。我我已将作用域设置为空数组,但推动它们的角度方式是什么,以便我可以通过ng repeat(即产品中的产品)访问它们AngularJS ng repeat:将对象推送到数组,angularjs,Angularjs,我有一个selectedProduct的对象。现在,我想创建这些selectedProduct对象的数组,以便使用ng repeat构建列表。我我已将作用域设置为空数组,但推动它们的角度方式是什么,以便我可以通过ng repeat(即产品中的产品)访问它们 $scope.products = []; $scope.getProduct = function() { ProductService.getProduct($scope.eanInput.ean)
$scope.products = [];
$scope.getProduct = function() {
ProductService.getProduct($scope.eanInput.ean)
.then(function(product) {
$scope.selectedProduct = product;
$scope.selectedProduct.ean = $scope.eanInput.ean;
$scope.selectedProduct.qtyInput = 1;
$scope.focusOn = 'qty';
$scope.eanInput.productFound = true;
})
.catch(function() {
$scope.eanInput.productFound = false;
});
};
老实说,我会把它们推到阵列上:
$scope.products = [];
$scope.getProduct = function() {
ProductService.getProduct($scope.eanInput.ean)
.then(function(product) {
$scope.selectedProduct = product;
$scope.selectedProduct.ean = $scope.eanInput.ean;
$scope.selectedProduct.qtyInput = 1;
$scope.focusOn = 'qty';
$scope.products.push(product)
$scope.eanInput.productFound = true;
})
.catch(function() {
$scope.eanInput.productFound = false;
});
};
然后在html中,您可以执行以下操作:
<div ng-controller="YourCtrl">
<h1 ng-repeat="product in products">
{{product.ean}}
</h1>
</div>
{{product.ean}}
据我所知,没有任何角度的方式将对象推入数组,您可以使用默认的javascript方式:
$scope.products.push(产品)代码>无论谁否决了这一点:请随意给我一个理由