我第一次使用Angular并遇到了尝试制作简单重装按钮的问题。我的网页只显示一个表,其数据是从Parse数据库中获取的。我还有一些其他按钮可以更改表格中的项目。移动后,我希望能够重新加载我的表并恢复以反映数据库,清除发生的任何更改。任何帮助将不胜感激!
$scope.reset = function() {
$scope.compOrder = [];
$scope.foundIndex = -1;
$scope.selectedItem = {};
$scope.citySelect($scope.currentCity);
}
以下是其他内容。请注意,函数citySelect是我用来从Parse中提取数据并填充模型的函数。
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;
}
}]);
答案 0 :(得分:0)
用户界面无法了解范围的变化,因此您必须致电$scope.$apply()
才能运行digest cycle。
$scope.reset = function() {
...
$timeout(function() {
$scope.$apply();
});
}
$timeout
用于在当前调用堆栈清除后运行,否则可能已经存在摘要循环。
查看此article以获取有关摘要周期的更多帮助。