gpt4 book ai didi

javascript - 使用一个函数来更新与给定 `id` 匹配的整页 ng-repeat 指令行

转载 作者:行者123 更新时间:2023-12-03 05:36:24 25 4
gpt4 key购买 nike

<!-- featured items table -->
<ul>
<li ng-repeat="item in items track by item.id">
<a href="" ng-click="addCart(item.id)">{{item.status}}</a>
</li>
</ul>

<!-- another tabs from same data table -->
<ul>
<li ng-repeat="item in items track by item.id">
<a href="" ng-click="addCart(item.id)">{{item.status}}</a>
</li>
</ul>

<script>
// controler
//items array here
// controller end

// start add to cart controller
$scope.addCart = function(id){
$scope.items[id].status = 'added';
}
//end of add to cart controller
</script>

我是 Angular JS 的新手,我的代码看起来像我上面写的那样,我只想创建一个函数 addtocart 一次,当我单击此函数时,我想替换整个页面行,其中 item.id 匹配任何 id,否则在 ng-repeat 内,无论 id 与 ng-repeat 匹配。当我尝试这样做时,我收到有关访问数组的错误,有时仅适用于 1 行。有什么想法建议我需要做什么吗?谢谢你

问候,阿桑·哈龙

最佳答案

service()函数在创建实例时会使用new关键字实例化该实例。

 var Person = function() {
var items = [{id:1,title:'test1'},{id:2,title:'test2'}];
module.service('MyService', function() {
this.getItems = function() {
return items; //.. get your item array
}

this.setItems = function() {
// Update your item array and return it.
return items;
}
});
};

angular.service('personService', Person);

关于javascript - 使用一个函数来更新与给定 `id` 匹配的整页 ng-repeat 指令行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40715548/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com