javascript - 将函数设置为 ng-model

标签 javascript jquery angularjs

我有一个复杂的数据结构(购物车的产品),如下所示:

items = [
    {
        id_product: 5,
        combinations: [
            {
                id_product_attribute: 35,
                quantity: 1,
                price: 25.60
            },
            {
                id_product_attribute: 38,
                quantity: 4,
                price: 25.60
            }
        ]
    },
    {
        id_product: 5,
        combinations: [
            {
                id_product_attribute: 35,
                quantity: 1,
                price: 28.60
            }
        ]
    }
];

我还有一个 ng-repeat,它在 li 元素中列出了从 $resource 检索到的产品的每个组合。每个 li 元素如下所示:

<li>Combination {{ combination.id }} - Price {{ combination.price }} - In your shopping cart {{ function_goes_here(product.id, combination.id) }}</li>

我想知道是否可以编写某种函数来在购物车数据结构中进行搜索(按产品 id 和组合 id)并返回数量元素,以这种方式,如果另一个元素会更新 View 相同类型的组合是否已添加到购物车?

最佳答案

在您的 $scope/scope 上定义该函数:

$scope.items = [
    {
        id_product: 5,
        combinations: [
            {
                id_product_attribute: 35,
                quantity: 1,
                price: 25.60
            },
            {
                id_product_attribute: 38,
                quantity: 4,
                price: 25.60
            }
        ]
    },
    {
        id_product: 5,
        combinations: [
            {
                id_product_attribute: 35,
                quantity: 1,
                price: 28.60
            }
        ]
    }
];

$scope.function_goes_here = function(productId, combinationId){
    //search through $scope.items
    return "Lorem Ipsum";
}

关于javascript - 将函数设置为 ng-model,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27018073/

相关文章:

javascript - 有没有办法用 console.log 或类似的东西跟踪无限循环?

angularjs - 在AngularJs上使用$ on收听范围广播的通配符

c# - 从 javascript 文件(.js 文件)调用 C# 函数(.cs 文件)

javascript - 使用 ember 获取 JSON 响应

javascript - div 是否出现在 TD 的 TD onclick 与 jquery/javascript 的右侧?

javascript - AngularJS 按选项/选择过滤

angularjs - 当我在 svg 中使用 AngularJS 指令时,结果不显示

javascript - 使用触摸屏将图像拖放到 HTML5 上

javascript - 错误处理响应 : TypeError: a. 替换不是函数 'With a catch'

javascript - 遵循 Google 文档后,无法从回调函数访问 $scope