AngularJS 带有按钮的单项更新
AngularJS single item update with button
好的,我在 Firebase 中有一个产品数组
products
-JpzIlrpRfXzx5HbZMw_
description: 'The description to the product'
image: 'The link to the product image'
likes: '0'
price: '15'
title: 'The product title
我想为每个产品添加一个 "like" 按钮。每次单击按钮时,它需要将上面的内容加一。
我已经想出了如何在您输入类似内容的地方进行输入,但这不是很友好。
<input type="text" ng-model="product.likes" ng-change="products.$save(product)"/>
我将如何着手将输入更改为按钮?当单击时,整数等产品增加 1。
应该很简单:
<input type="button" ng-click="product.likes += 1; products.$save(product);"/>
注意:我建议你把它提取成一个函数
在您的函数中,只需将 product.likes
递增 1,然后 post 您的产品对象返回到您的数据库
<button type="button" ng-click="products.$save(product)">Save </button>
<p>{{product.likes}}</p>
JS:
$scope.products = {
$save : function(product) {
product.likes += 1;
//post your product
}
}
好的,我在 Firebase 中有一个产品数组
products
-JpzIlrpRfXzx5HbZMw_
description: 'The description to the product'
image: 'The link to the product image'
likes: '0'
price: '15'
title: 'The product title
我想为每个产品添加一个 "like" 按钮。每次单击按钮时,它需要将上面的内容加一。
我已经想出了如何在您输入类似内容的地方进行输入,但这不是很友好。
<input type="text" ng-model="product.likes" ng-change="products.$save(product)"/>
我将如何着手将输入更改为按钮?当单击时,整数等产品增加 1。
应该很简单:
<input type="button" ng-click="product.likes += 1; products.$save(product);"/>
注意:我建议你把它提取成一个函数
在您的函数中,只需将 product.likes
递增 1,然后 post 您的产品对象返回到您的数据库
<button type="button" ng-click="products.$save(product)">Save </button>
<p>{{product.likes}}</p>
JS:
$scope.products = {
$save : function(product) {
product.likes += 1;
//post your product
}
}