将角度scop绑定为javascript参数
我想将一些动态值绑定到我的javascript函数调用将角度scop绑定为javascript参数,javascript,angularjs,data-binding,Javascript,Angularjs,Data Binding,我想将一些动态值绑定到我的javascript函数调用 <button type="button" class="btn btn-info pull-right" onclick="addToCart('{{i.item_name}}','{{i.price}}')">Add to Cart</button> AngularJ使用ng click而不是onclick <button type="button" class="btn btn-info pull-ri
<button type="button" class="btn btn-info pull-right" onclick="addToCart('{{i.item_name}}','{{i.price}}')">Add to Cart</button>
AngularJ使用ng click而不是onclick
<button type="button" class="btn btn-info pull-right" ng-click="addToCart('{{i.item_name}}','{{i.price}}')">Add to Cart</button>
添加到购物车
angularjs使用ng单击而不是一次单击
<button type="button" class="btn btn-info pull-right" ng-click="addToCart('{{i.item_name}}','{{i.price}}')">Add to Cart</button>
添加到购物车
我猜I
来自ng repeat
?下面就可以了
ng click=“addToCart(i.item\u name,i.price)”
我猜i
来自ng repeat
?下面就可以了
ng click=“addToCart(i.item\u name,i.price)”
解决方案1:
您可以在javascript方法中将这些动态值分配给data属性和read data属性
function addToCart(e){
console.log(e.target.dataset);
//{itemName:'...', itemPrice:'...'}
}
<button type="button" class="btn btn-info pull-right" data-item-name="{{i.item_name}}" data-item-price="{{i.price}}" onclick="addToCart(event)">Add to Cart</button>
函数addToCart(e){
console.log(e.target.dataset);
//{itemName:'…',itemPrice:'…'}
}
添加到购物车
解决方案2:
在控制器中编写一个方法,并从那里调用javascript方法
//javascript method
function addToCart(itemName, itemPrice){
//your logic here
}
//inside controller
$scope.addToCart = function(item){
//javascript method call
addToCart(item.item_name, item.price);
};
//html
<button type="button" class="btn btn-info pull-right" ng-click="addToCart(i)">Add to Cart</button>
//javascript方法
函数addToCart(itemName、itemPrice){
//你的逻辑在这里
}
//内部控制器
$scope.addToCart=函数(项){
//javascript方法调用
addToCart(item.item\u名称、item.price);
};
//html
添加到购物车
解决方案1:
您可以在javascript方法中将这些动态值分配给data属性和read data属性
function addToCart(e){
console.log(e.target.dataset);
//{itemName:'...', itemPrice:'...'}
}
<button type="button" class="btn btn-info pull-right" data-item-name="{{i.item_name}}" data-item-price="{{i.price}}" onclick="addToCart(event)">Add to Cart</button>
函数addToCart(e){
console.log(e.target.dataset);
//{itemName:'…',itemPrice:'…'}
}
添加到购物车
解决方案2:
在控制器中编写一个方法,并从那里调用javascript方法
//javascript method
function addToCart(itemName, itemPrice){
//your logic here
}
//inside controller
$scope.addToCart = function(item){
//javascript method call
addToCart(item.item_name, item.price);
};
//html
<button type="button" class="btn btn-info pull-right" ng-click="addToCart(i)">Add to Cart</button>
//javascript方法
函数addToCart(itemName、itemPrice){
//你的逻辑在这里
}
//内部控制器
$scope.addToCart=函数(项){
//javascript方法调用
addToCart(item.item\u名称、item.price);
};
//html
添加到购物车