简体   繁体   English

如何在angular js中使用ng-model将文本框值发送到控制器

[英]How to send text box value to the controller using ng-model in angular js

I want to refresh the method $scope.getPIRData dynamically according to the text box value , I have one text box where I can give some seconds like 3000 ms , that I need to get into the setInterval block but my text box values not setting to the window.refreshtime. 我想根据文本框的值动态刷新方法$scope.getPIRData ,我有一个文本框,可以输入3000 ms之类的几秒钟,我需要进入setInterval块,但我的文本框值未设置为window.refreshtime.

Method is refreshing properly but after selecting the dropdown list the refresh mechanism not working before selecting the dropdown only it is working fine. 方法正在正确刷新,但是在选择下拉列表后,刷新机制不起作用,然后选择下拉列表,只有它可以正常工作。

html 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 script 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();
    });

use setTimeout over setInterval to get more control over the execution ( https://weblogs.asp.net/bleroy/setinterval-is-moderately-evil ). setInterval上使用setTimeout可以更好地控制执行( https://weblogs.asp.net/bleroy/setinterval-is-moderately-evil )。

AngualrJs has inbuilt $timeout service, which takes care of the digest cycle. AngualrJs内置了$timeout服务,该服务负责摘要循环。

 var app = angular.module('PIR_Detection', []); app.controller('myCtrl', function ($scope, $http, $window, $timeout) { $scope.sel_val = 0; $scope.DefaultLabel = "Loading....."; $scope.refreshtime = 1000; // commenting the data code, just for the solution demo /* var post = $http({ method: "get", url: "../data.json", dataType: 'json', data: {}, headers: { "Content-Type": "application/json" } }); post.then(function (data, status) { $scope.pirs = data; }); post.catch(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.refreshPIR); }; 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(); }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script> <div ng-app="PIR_Detection" ng-controller="myCtrl"> Refresh interval: <!-- Used ng-model-options to fire the change event once user comes out the textbox--> <input type="number" ng-model="refreshtime" ng-model-options="{ updateOn: 'blur' }" ng-change="refreshPIR()" id="txtRefresh" name="name"/> </div> 

Ok so two things... first of all, note the misspelling in $scope.refrestime . 好了,有两件事...首先,请注意$scope.refrestime的拼写错误。 That being said, this won't fix your problem. 话虽如此,这不会解决您的问题。 setInterval's interval is set when you call it and cannot be changed. setInterval的间隔是在调用时设置的,无法更改。

Seeing as you seem to want to only change the interval on the next time the interval hits, What you can do is pull the function out of the setInterval and into its own reference and instead of setInterval simply use setTimeout and then as the last line of the function just call setTimeout(refreshPIR, $scope.refreshtime) 似乎您似乎只想在下一次击中间隔时更改间隔,您可以做的是将函数从setInterval中拉出并放入其自己的引用中,而不是setInterval,而只需使用setTimeout,然后将其用作最后一行该函数只调用setTimeout(refreshPIR,$ scope.refreshtime)

function refreshPIR() {
  if (window.deviceId != null) {
    $scope.getPIRData(window.deviceId);
  }
  setTimeout(refreshPIR, $scope.refreshtime);
}

setTimeout(refreshPIR, window.refreshtime);

Tho you might want to add some error handling to make sure scope.refreshtime is an int :-) 您可能想要添加一些错误处理以确保scope.refreshtime是一个int :-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM