【问题标题】:Angularjs must refresh page to see changesAngularjs 必须刷新页面才能看到更改
【发布时间】:2013-08-19 10:20:01
【问题描述】:

我所拥有的是简单的 CRUD 操作。项目在页面上列出,当用户点击按钮添加时,弹出模式,用户输入数据,数据被保存并自动(无需刷新)添加到页面列表中。

服务:

getAllIncluding: function(controllerAction, including) {
            var query = breeze.EntityQuery.from(controllerAction).expand(including);
            return manager.executeQuery(query).fail(getFailed);
        }, 

addExerciseAndCategories: function(data, initialValues) {
            var addedExercise = manager.createEntity("Exercise", initialValues);
            _.forEach(data, function(item) {
                manager.createEntity("ExerciseAndCategory", { ExerciseId: addedExercise._backingStore.ExerciseId, CategoryId: item.CategoryId });
            });
            saveChanges().fail(addFailed);

            function addFailed() {
                removeItem(items, item);
            }
        },

控制器:

 $scope.getAllExercisesAndCategories = function() {
        adminCrudService.getAllIncluding("ExercisesAndCategories", "Exercise,ExerciseCategory")
            .then(querySucceeded)
            .fail(queryFailed);

    };

     function querySucceeded(data) {

            $scope.queryItems = adminCrudService.querySucceeded(data);

            var exerciseIds = _($scope.queryItems).pluck('ExerciseId').uniq().valueOf();
            $scope.exerciseAndCategories = [];
            var createItem = function (id, exercise) {
                return {
                    ExerciseId: id,
                    Exercise : exercise,
                    ExerciseCategories: []
                };
            };

            // cycle through ids
            _.forEach(exerciseIds, function (id) {
                // get all the queryItems that match
                var temp = _.where($scope.queryItems, {
                    'ExerciseId': id
                });
                // go to the next if nothing was found.
                if (!temp.length) return;
                // create a new (clean) item
                var newItem = createItem(temp[0].ExerciseId, temp[0].Exercise);
                // loop through the queryItems that matched
                _.forEach(temp, function (i) {
                    // if the category has not been added , add it.
                    if (_.indexOf(newItem.ExerciseCategories, i.ExerciseCategory) < 0) {
                        newItem.ExerciseCategories.push(i.ExerciseCategory);
                    }
                });
                // Add the item to the collection
                $scope.items.push(newItem);
            });


            $scope.$apply();

        }

这是我从控制器添加新数据的方法:

 adminCrudService.addExerciseAndCategories($scope.selectedCategories, { Name: $scope.NewName, Description: $scope.NewDesc });

所以我的问题是,为什么列表没有实时更新(当我点击保存时,我必须刷新页面)。

编辑

这是我的查询成功

querySucceeded: function (data) {
            items = [];
            data.results.forEach(function(item) {
                items.push(item);
            });
            return items;


        }

编辑 2

我相信我已经缩小了我的问题范围!

所以PW Kad 花了两个小时试图帮助我解决这个问题(我非常非常非常感谢他),但不幸的是没有成功。我们主要尝试修复我的服务,所以当我回到我的电脑时,我再次尝试修复它。我相信我的服务很好。 (正如 Kad 在他的回答中建议的那样,我做了一些改变)。 我相信问题出在控制器中,我已经记录了 $scope.items,当我添加新项目时它们没有改变,之后我记录了 $scope.queryItems,我注意到它们在添加新项目后发生了变化项目(无刷新)。因此,在加载初始数据后,可能会通过某种方式 $watching $scope.queryItems 解决问题,但目前我不太确定如何执行此操作。

【问题讨论】:

  • 请注意,您发布的代码绝对是大量的,而且挖掘所有代码非常耗时。如果您将其简化为您遇到问题的代码,确定问题发生的位置并寻求具体帮助,您可能会取得更大的成功。
  • 您的服务上的 querySucceeded 功能在哪里?为什么要取回结果然后将其发送回服务?
  • 还有为什么需要调用scope.apply?
  • 让我们用这个快速聊天......

标签: javascript angularjs asp.net-web-api breeze


【解决方案1】:

所以我已经设法解决了我的问题!不确定这是否是正确的解决方案,但现在可以使用。 我已将所有内容移至我的服务,现在看起来像这样:

function addCategoriesToExercise(tempdata) {
        var dataToReturn = [];
        var exerciseIds = _(tempdata).pluck('ExerciseId').uniq().valueOf();
        var createItem = function (id, exercise) {
            return {
                ExerciseId: id,
                Exercise: exercise,
                ExerciseCategories: []
            };
        };

        // cycle through ids
        _.forEach(exerciseIds, function (id) {
            // get all the queryItems that match
            var temp = _.where(tempdata, {
                'ExerciseId': id
            });
            // go to the next if nothing was found.
            if (!temp.length) return;
            // create a new (clean) item
            var newItem = createItem(temp[0].ExerciseId, temp[0].Exercise);
            // loop through the queryItems that matched
            _.forEach(temp, function (i) {
                // if the category has not been added , add it.
                if (_.indexOf(newItem.ExerciseCategories, i.ExerciseCategory) < 0) {
                    newItem.ExerciseCategories.push(i.ExerciseCategory);
                }
            });
            // Add the item to the collection
            dataToReturn.push(newItem);
        });

        return dataToReturn;
    }

    addExerciseAndCategories: function (data, initialValues) {
        newItems = [];

        var addedExercise = manager.createEntity("Exercise", initialValues);

        _.forEach(data, function (item) {
            var entity = manager.createEntity("ExerciseAndCategory", { ExerciseId: addedExercise._backingStore.ExerciseId, CategoryId: item.CategoryId });
            items.push(entity);
            newItems.push(entity);

        });
        saveChanges().fail(addFailed);

        var itemsToAdd = addCategoriesToExercise(newItems);

        _.forEach(itemsToAdd, function (item) {
            exerciseAndCategories.push(item);
        });

        function addFailed() {
            removeItem(items, item);
        }
    }

 getAllExercisesAndCategories: function () {
            var query = breeze.EntityQuery.from("ExercisesAndCategories").expand("Exercise,ExerciseCategory");
            return manager.executeQuery(query).then(getSuceeded).fail(getFailed);

        },

function getSuceeded(data) {
        items = [];
        data.results.forEach(function (item) {
            items.push(item);
        });

        exerciseAndCategories = addCategoriesToExercise(items);

        return exerciseAndCategories;
    }

而在控制器中我只有这个:

   $scope.getAllExercisesAndCategories = function () {
        adminExerciseService.getAllExercisesAndCategories()
            .then(querySucceeded)
            .fail(queryFailed);

    };
      function querySucceeded(data) {
            $scope.items = data;

            $scope.$apply();

        }

【讨论】:

    【解决方案2】:

    好的,我将发布一个答案,以指导您如何解决您的问题。问题似乎不在于 Breeze,也不在于 Angular,而在于您将两者结合起来的方式。我这样说是因为了解你在做什么对于理解调试过程很重要。

    创建一个实体会将其添加到缓存中,并且 entityState 为 isAdded - 这是一个正确的说法,不要有其他想法。

    现在为您的代码...

    您没有将查询执行与承诺联系起来,但在您的情况下,您将数据返回到控制器,然后将其直接传递回服务中的某个函数,您的问题中没有列出。我添加了一个函数来复制你的可能的样子。

    getAllIncluding: function(controllerAction, including) {
                var query = breeze.EntityQuery.from(controllerAction).expand(including);
                return manager.executeQuery(query).then(querySucceeded).fail(getFailed);
    
                function querySucceeded(data) {
                    return data.results;
                }
            }, 
    

    现在在您的控制器中简单地处理结果 -

    $scope.getAllExercisesAndCategories = function() {
        adminCrudService.getAllIncluding("ExercisesAndCategories", "Exercise,ExerciseCategory")
            .then(querySucceeded)
            .fail(queryFailed);
    };
    
     function querySucceeded(data) {
            // Set your object directly to the data.results, because that is what we are returning from the service
            $scope.queryItems = data;  
            $scope.exerciseAndCategories = [];
    

    最后,让我们添加我们创建实体的属性,看看这是否让 Angular 有机会正确绑定 -

    _.forEach(data, function(item) { 
        var e = manager.createEntity("ExerciseAndCategory"); 
        e.Exercise = addedExercise; e.Category: item.Category; 
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-18
      • 1970-01-01
      • 2021-09-24
      • 2014-05-24
      相关资源
      最近更新 更多