Javascript 为什么赢了';尝试重新加载数据时,我的$scope字段是否清除?

Javascript 为什么赢了';尝试重新加载数据时,我的$scope字段是否清除?,javascript,angularjs,parse-platform,Javascript,Angularjs,Parse Platform,我第一次使用Angular,在尝试制作一个简单的重新加载按钮时遇到了一个问题。我的网页只是显示了一个表,它的数据是从解析数据库中获取的。我还有一些其他的按钮可以改变表中的项目。在移动内容之后,我希望能够重新加载表并进行恢复以反映数据库,从而清除发生的任何更改。任何帮助都将不胜感激 $scope.reset = function() { $scope.compOrder = []; $scope.foundIndex = -1; $scope.selectedItem =

我第一次使用Angular,在尝试制作一个简单的重新加载按钮时遇到了一个问题。我的网页只是显示了一个表,它的数据是从解析数据库中获取的。我还有一些其他的按钮可以改变表中的项目。在移动内容之后,我希望能够重新加载表并进行恢复以反映数据库,从而清除发生的任何更改。任何帮助都将不胜感激

$scope.reset = function() {
    $scope.compOrder = [];
    $scope.foundIndex = -1;
    $scope.selectedItem = {};
    $scope.citySelect($scope.currentCity);
}
下面是其他内容,以查看其他上下文。请注意,citySelect函数是我用来从解析中提取数据并填充模型的函数

var myApp = angular.module('CompetitionManager',[]);

myApp.controller('MainController', ['$scope', function($scope) {

    $scope.availableCities = [];
    $scope.compOrder = [];
    $scope.selectedItem = {};
    $scope.foundIndex = -1;
    $scope.currentCity = {};

    $scope.init = function() {
        Parse.initialize("XXX", "XXX");
        Parse.Cloud.run("checkAllCities", {}, {
            success: function(data) {
                var cityArr = [];
                for (var i = 0; i < data.length; i++) {
                    if(data[i].get("uploaded")) {
                        $scope.availableCities.push(data[i]);
                        $scope.$apply()
                    }
                }
            },
            error: function(e) {
                alert("Failed Loading Cities");
            }
        })
    }

    $scope.citySelect = function(city) {
        $scope.currentCity = city;
        try {
            var cityObj = JSON.parse(city);
        }
        catch (e) {
            $scope.compOrder = [];
            return;
        }
        var cityName = cityObj["city"];
        Parse.Cloud.run("getCityCompetition", {city : cityName}, {
            success: function(data) {
                $scope.compOrder = $scope.compOrder.concat(data);
                $scope.$apply()
            },
            error: function(e) {
                alert(e);
            }
        });
    }

    $scope.rowClicked = function(item) {
        $scope.selectedItem = item;
        $scope.foundIndex = $scope.compOrder.indexOf(item);
    }

    $scope.swapUp = function() {
        if($scope.foundIndex > 0) {
            swapElements($scope.compOrder, $scope.foundIndex-1, $scope.foundIndex);
            $scope.foundIndex--;
        }
    }

    $scope.swapDown = function() {
        if ($scope.foundIndex < $scope.compOrder.length-1 && $scope.foundIndex >= 0) {
            swapElements($scope.compOrder, $scope.foundIndex, $scope.foundIndex+1);
            $scope.foundIndex++;
        }
    }

    $scope.keyPress = function(event, item) {
        if(event.which != 13) {
            return;
        }
        var value = event["srcElement"]["value"]
        if (!value) {
            return;
        }
        var index = $scope.compOrder.indexOf(item);
        var firstItem = $scope.compOrder[index];
        firstItem.set("length", [0, 0, 0, 0, value, 0]);
        for(var i = index + 1; i < $scope.compOrder.length; i++) {
            var currentItem = $scope.compOrder[i];
            var currentTime = currentItem.get("startTime");
            var lastItem = $scope.compOrder[i - 1];
            var lastLength = lastItem.get("length")[4];
            var lastTime = lastItem.get("startTime");
            currentTime.setTime(lastTime.getTime() + lastLength*60*1000);
        }
    }

    $scope.reset = function() {
        $scope.compOrder = [];
        $scope.foundIndex = -1;
        $scope.selectedItem = {};
        $scope.citySelect($scope.currentCity);
    }

    var swapElements = function swapElements(arr, index1, index2) {
        var firstTime = new Date(arr[index1].get("startTime").getTime());
        var firstLength = arr[index2].get("length")[4];
        var secondTime = new Date(firstTime.getTime());
        secondTime.setMinutes(secondTime.getMinutes() + firstLength);
        arr[index1].set("startTime", secondTime);
        arr[index2].set("startTime", firstTime);
        var temp = arr[index1];
        arr[index1] = arr[index2];
        arr[index2] = temp;
    }
}]);
var myApp=angular.module('CompetitionManager',[]);
myApp.controller('MainController',['$scope',函数($scope){
$scope.availableCities=[];
$scope.compOrder=[];
$scope.selectedItem={};
$scope.foundIndex=-1;
$scope.currentCity={};
$scope.init=函数(){
初始化(“XXX”、“XXX”);
run(“checkAllCities”,{}{
成功:功能(数据){
var cityar=[];
对于(变量i=0;i0){
交换项($scope.compOrder、$scope.foundIndex-1、$scope.foundIndex);
$scope.foundIndex--;
}
}
$scope.swapDown=函数(){
如果($scope.foundIndex<$scope.compOrder.length-1&&$scope.foundIndex>=0){
交换项($scope.compOrder、$scope.foundIndex、$scope.foundIndex+1);
$scope.foundIndex++;
}
}
$scope.keyPress=功能(事件、项目){
if(event.which!=13){
返回;
}
变量值=事件[“srcelelement”][“value”]
如果(!值){
返回;
}
变量索引=$scope.compOrder.indexOf(项目);
var firstItem=$scope.compOrder[index];
set(“长度”、[0,0,0,0,0,value,0]);
对于(变量i=索引+1;i<$scope.compOrder.length;i++){
var currentItem=$scope.compOrder[i];
var currentTime=currentItem.get(“开始时间”);
var lastItem=$scope.compOrder[i-1];
var lastLength=lastItem.get(“长度”)[4];
var lastTime=lastItem.get(“startTime”);
currentTime.setTime(lastTime.getTime()+lastLength*60*1000);
}
}
$scope.reset=函数(){
$scope.compOrder=[];
$scope.foundIndex=-1;
$scope.selectedItem={};
$scope.citySelect($scope.currentCity);
}
变量交换=功能交换(arr、index1、index2){
var firstTime=新日期(arr[index1].get(“startTime”).getTime();
var firstLength=arr[index2]。获取(“长度”)[4];
var secondTime=新日期(firstTime.getTime());
secondTime.setMinutes(secondTime.getMinutes()+firstLength);
arr[index1].set(“开始时间”,第二次);
arr[index2].set(“开始时间”,第一次);
var-temp=arr[index1];
arr[index1]=arr[index2];
arr[index2]=温度;
}
}]);

用户界面不知道范围的更改,因此必须调用
$scope.$apply()
才能运行

$timeout
用于在清除当前调用堆栈后运行,否则可能已经有摘要循环在运行


有关摘要周期的更多帮助,请查看此页。

到底是什么不起作用?你能创造一把小提琴来展示这个问题吗?
$scope.reset = function() {
    ...

    $timeout(function() {
       $scope.$apply();
    });
}