javascript - Angular JS ng-repeat 元素不更新

标签 javascript angularjs

我必须显示产品列表,并能够单击它们以获取有关它们的更多信息。

我的产品列表存储在名为“productsServices”的服务中

我有一个 html 页面显示我的所有产品,另一个显示 1 个产品的详细信息。每个 html 页面都有自己的 Controller 。

当我在详细信息页面中更新产品时,更改会在我的 productsServices 中完成,但不会在我的全局产品 View 中进行验证。

(function ()
{
    angular.module('app').service('productsServices', ['$http', '$q', ProductsServices])
    that = this
    
    that.products           = /*arrayOfData*/;
    that.getProducts        = function () {
                                return that.products;
                              }
})();


(function ()
{
    angular.module('app').controller('ProductsController', ['$http', '$q',  'productsServices', ProductsController])
    
    that                   = this
    that.products          = productsServices.getProducts;
    
    that.showDetail        = function (item) {
                                $state.go('menu.product', { myParam: item });
                             }
})();
<!--Global HTML page-->
<ion-content id="content" class="has-header has-subheader" delegate-handle="myScroll">
        <ion-list>
            <ion-item ng-repeat="product in that.products() track by product.ProductId" href="#" ng-click="that.showDetail({{product}})">
                <div class="innerProd">
                  <img id="productPicture" name="productPicture" ng-src='data:image/png;base64,{{product.ThumbnailPhoto}}'></div>
                <div class= "innerProd">
                    <p>{{product.Name}}</p>
                    <p>{{product.Price}}$</p>
                    <ion-option-button class="button button-assertive"
                                        ng-click="doDelete({{product}})">
                        Supprimer
                    </ion-option-button>
                </div>
            </ion-item>
        </ion-list>
        <ion-infinite-scroll ng-if="moreDataCanBeLoaded()" icon="ion-loading-c" on-infinite="loadMoreData()" distance="100%">
        </ion-infinite-scroll>
</ion-content>

因此,当我检查“that.showDetail”方法中发送的产品时,即使产品已在服务中更新,发送的也是旧产品。

我不是英语,所以很抱歉我的句子中出现语言错误^^'

提前感谢您的帮助^^'

最佳答案

好的,我找到了问题的答案,我已经替换了发送到“that.showDetail”函数的参数。我不再直接发送对象,而是发送我的项目的索引。

<ion-item ng-repeat="product in that.products() track by product.ProductId" href="#" ng-click="that.showDetail({{$index}})">

我猜 Angular 不会更新在 ng-click 调用中注入(inject)的内容 ^^'

谢谢大家的宝贵时间

关于javascript - Angular JS ng-repeat 元素不更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37746120/

相关文章:

javascript - 如何让onClick同时执行多个类似的功能?

javascript - 过滤AngularJS中的数组值

javascript - 如何根据在angularjs中选择的性别切换图像

javascript - $scope.$watch 未捕获指令修改的值

javascript - 如何在 AngularJS 中连接 jsReport?

javascript - Angular : Show div content depending on 2 stage drop down menu

javascript - 自动加载下拉菜单选项

javascript - 将带有复选框的列动态添加到表单中的html表

javascript - AngularJS - jQuery 返回元素集合

javascript - Laravel 删除按钮不适用于 Ajax 调用