Javascript 如何使用angular js中的ng模型向控制器发送文本框值
我想根据文本框值动态刷新方法Javascript 如何使用angular js中的ng模型向控制器发送文本框值,javascript,html,angularjs,Javascript,Html,Angularjs,我想根据文本框值动态刷新方法$scope.getPIRData,我有一个文本框,我可以给它一些秒,比如3000毫秒,我需要进入setInterval块,但是我的文本框值没有设置到窗口。刷新时间。 方法正在正确刷新,但在选择下拉列表后,刷新机制不工作,在选择下拉列表之前,刷新机制工作正常 html 好的,所以有两件事。。。首先,请注意$scope.refrestime中的拼写错误。 尽管如此,这并不能解决你的问题。setInterval的间隔是在调用时设置的,不能更改 鉴于您似乎只想在下次间隔到达
$scope.getPIRData
,我有一个文本框,我可以给它一些秒,比如3000毫秒,我需要进入setInterval
块,但是我的文本框值没有设置到窗口。刷新时间。
方法正在正确刷新,但在选择下拉列表后,刷新机制不工作,在选择下拉列表之前,刷新机制工作正常
html
好的,所以有两件事。。。首先,请注意
$scope.refrestime
中的拼写错误。
尽管如此,这并不能解决你的问题。setInterval的间隔是在调用时设置的,不能更改
鉴于您似乎只想在下次间隔到达时更改间隔,您可以做的是将函数从setInterval中拉出来并放入它自己的引用中,而不是使用setInterval,只需使用setTimeout,然后作为函数的最后一行调用setTimeout(refreshPIR,$scope.refreshtTime)
t您可能需要添加一些错误处理,以确保scope.refreshtime是int:-)使用
setTimeout
oversetInterval
来获得对执行的更多控制()
AngualrJs具有内置的$timeout
服务,它负责摘要周期
var-app=angular.module('PIR_-Detection',[]);
app.controller('myCtrl',函数($scope、$http、$window、$timeout){
$scope.sel_val=0;
$scope.DefaultLabel=“正在加载…”;
$scope.refreshtime=1000;
//注释数据代码,仅用于解决方案演示
/*var post=$http({
方法:“获取”,
url:“../data.json”,
数据类型:“json”,
数据:{},
标题:{“内容类型”:“应用程序/json”}
});
post.then(功能(数据、状态){
$scope.pirs=数据;
});
post.catch(功能(数据、状态){
}); */
$scope.getPIRData=函数(id){
var url=“/PIRDetails/GetPIRStatus/”+id;
$http.get(url)
.然后(功能(响应){
$scope.myWelcome=response.data;
如果($scope.myWelcome!=“”){
$scope.pirstatus=base64toHEX($scope.myWelcome.dataFrame);
}
$window.deviceId=id;
})
//请求后将进行下一次呼叫
.最后($scope.refreshPIR);
};
让timeOut=null;
$scope.refreshPIR=函数(){
如果(超时){
//从事件循环中删除上一个超时
$timeout.cancel(超时);
}
log('timeout方法调用在',$scope.refreshtime,'ms');
timeOut=$timeOut(函数(){
如果($window.deviceId){
$scope.getPIRData($window.deviceId);
}否则{
$scope.refreshPIR();
}
},$scope.refreshttime);
};
//初始化
$scope.refreshPIR();
});代码>
刷新间隔:
它几乎可以正常工作,但关键是它可以加载页面。选择下拉列表刷新方法不工作后,一旦选择下拉列表,console.log将不显示,是否可以检查我的更新code@krishnamohan它应该是$scope.refreshPIR
中的.finally
-更正了我的代码,请更新并再次尝试感谢这么好的代码和支持。你能告诉我代码中使用的ng change=“setupInterval()在哪里吗?我找不到这个ng change的用法event@krishnamohan更正了我的代码,最初,我将函数名命名为setupInterval
,然后重命名为refreshPIR
-忘记了到处重命名..现在更新了代码。
<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>
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();
});
function refreshPIR() {
if (window.deviceId != null) {
$scope.getPIRData(window.deviceId);
}
setTimeout(refreshPIR, $scope.refreshtime);
}
setTimeout(refreshPIR, window.refreshtime);