Я хочу обновить метод $scope.getPIRData
динамически в соответствии со значением текстового поля. У меня есть одно текстовое поле, в котором я могу дать несколько секунд, например 3000 мс, которые мне нужны, чтобы попасть в блок setInterval
, кроме моего текстового поля.значения, для которых не установлен window.refreshtime.
метод обновляется правильно, но после выбора раскрывающегося списка механизм обновления не работает, а только после выбора раскрывающегося списка работает нормально.
html
<input type="number"
ng-model="refreshtime"
ng-model-options="{ updateOn: 'blur' }"
ng-change="setupInterval()"
id="txtRefresh" name="name" />
<select class="form-control ng-pristine ng-valid ng-scope ng-empty ng-touched" ng-model="sel_val" ng-change="getPIRData(sel_val.deveui)" ng-options="data.details for data in pirs">Select PIR Device</select>
Java-скрипт
var app = angular.module('PIR_Detection', []);
app.controller('myCtrl', function ($scope, $http, $window, $timeout) {
$scope.sel_val = 0;
$scope.DefaultLabel = "Loading.....";
$scope.refreshtime = 1000;
var post = $http({
method: "get",
url: "../data.json",
dataType: 'json',
data: {},
headers: { "Content-Type": "application/json" }
});
post.success(function (data, status) {
$scope.pirs = data;
});
post.error(function (data, status) {
});
$scope.getPIRData = function (id) {
var url = "/PIRDetails/GetPIRStatus/" + id;
$http.get(url)
.then(function (response) {
$scope.myWelcome = response.data;
if ($scope.myWelcome != "") {
$scope.pirstatus = base64toHEX($scope.myWelcome.dataFrame);
}
$window.deviceId = id;
})
// next call will be made after the request
.finally($scope.setupInterval);
};
let timeOut = null;
$scope.refreshPIR = function () {
if (timeOut) {
// removes the previous timeout from event loop
$timeout.cancel(timeOut);
}
console.log('timeout method call at ', $scope.refreshtime, 'ms');
timeOut = $timeout(function () {
if ($window.deviceId) {
$scope.getPIRData($window.deviceId);
} else {
$scope.refreshPIR();
}
}, $scope.refreshtime);
};
//init
$scope.refreshPIR();
});