我正在尝试引用数组中的一项,但我不知道为什么这不起作用,
console.log($scope.Times);
console.log($scope.Times[0]);
这两行代码完全在彼此之后,但我从控制台得到的输出如下..
为什么这不起作用的任何想法?正如我之前提到的,这些命令完全在彼此之后,并且在同一个函数中,变量在我的 Controller 中是全局的。
如果您认为有帮助,我可以添加更多代码,但我真的不明白如何......
更多代码:
$scope.Times = [];
$scope.getStatus = function(timer){
$http.post('getStatus.php', {timer : timer})
.success(function(response){
$scope.response = response;
if ($scope.response.Running === "0"){
$scope.model = { ItemNumber : $scope.response.Part };
$scope.loadTiming($scope.response.Part);
console.log($scope.Times);
console.log($scope.Times[0]);
}
});
};
$scope.loadTiming = function(itemNumber) {
$http.post('getPartTimings.php', {itemNumber : itemNumber})
.success(function(response){
$scope.selectedTiming = response;
$scope.Times.splice(0);
var i = 0;
angular.forEach($scope.selectedTiming, function(value) {
if (value !== 0)
$scope.Times.push({
"Process" : $scope.procedures[i],
"Duration" : value*60
});
i++;
});
});
};
<?php
$postData = file_get_contents("php://input");
$request = json_decode($postData);
require "conf/config.php";
mysqli_report(MYSQLI_REPORT_STRICT);
try {
$con=mysqli_connect(DBSERVER,DBUSER,DBPASS,DBNAME);
} catch (Exception $exp) {
echo "<label style='font-weight:bold; color:red'>MySQL Server Connection Failed. </label>";
exit;
}
$query = 'SELECT *,
TIME_TO_SEC(TIMEDIFF(NOW(),Timestamp))
FROM live_Timers
WHERE Timer='.$request->timer;
$result = mysqli_query($con, $query);
$data = mysqli_fetch_assoc($result);
echo JSON_ENCODE($data);
感谢您的帮助。
最佳答案
好的,所以更多的代码确实有帮助。看起来你在这里发生了异步逻辑。 loadTiming 被触发,它执行 POST
,然后在 Times 数组上进行拼接。一个 console.log 可能在此 POST
之前触发,另一个在之后触发。没有简单的方法可以分辨。
一个可能的修复方法是仅在 loadTiming
异步进程运行时才记录这些。从 loadTiming
函数返回一个 promise ,然后在 promise 的 then 回调中记录您的数组。
$scope.getStatus = function(timer){
$http.post('getStatus.php', {timer : timer})
.success(function(response){
$scope.response = response;
if ($scope.response.Running === "0"){
$scope.model = { ItemNumber : $scope.response.Part };
$scope.loadTiming($scope.response.Part).then(function () {
console.log($scope.Times);
console.log($scope.Times[0]);
});
}
});
};
$scope.loadTiming = function(itemNumber) {
return $http.post('getPartTimings.php', {itemNumber : itemNumber})
.success(function(response){
$scope.selectedTiming = response;
$scope.Times.splice(0);
var i = 0;
angular.forEach($scope.selectedTiming, function(value) {
if (value !== 0)
$scope.Times.push({
"Process" : $scope.procedures[i],
"Duration" : value*60
});
i++;
});
});
};
关于javascript - 数组引用 javascript angular,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26996783/