在AngularJS中,如何防止选中的选项和文本框在按下F5键后被清除,直到按钮被点击

How to prevent a selected option and a textbox from being cleared after pressing F5 In AngularJS, until a button is clicked

本文关键字:清除 按钮 F5 何防止 AngularJS 选项 文本      更新时间:2023-09-26

我有一个选择选项和一个文本框,当用户选择一个选项并在文本框中输入文本,数据被保存在sql数据库中。我需要知道如何防止选中的选项和当用户按下F5键时,文本框中的文本不会被清除,直到在AngularJS中单击某个按钮,然后select选项和文本框才会被清除。这是我试过的,但它不起作用。

JS

function Model($http) {
    var self = this;
    self.$http = $http;
    var CurrentDowntimeEvent = {};
  self.StartDowntimeEvent = function () {      
        var params= {
              DepartmentId:self.LineId
            , EventId: self.CurrentDowntimeEvent.EventId
            , CategoryId: self.CurrentDowntimeEvent.CategoryId
            , StartTime: self.CurrentDowntimeEvent.startTime
            , Comment: self.CurrentDowntimeEvent.Comment
        };
        Hesto.Ajax.ngget(self.$http, ADD_DOWNTIME_START, params, function (data) {
            self.CurrentDowntimeEvent = data.data[0];
            console.log(self.CurrentDowntimeEvent);
        });
    }
angular.module('myApp', [])
 .controller('DowntimeController', function ($scope, $http) {
     $scope.Model = new Model($http);
});
The CategoryId is the select option and the  Comment is the textbox.
HTML

 <select class="categories" ng-disabled="selectlistdisabled" ng-model="Model.CurrentDowntimeEvent.CategoryId" ng-click="display()">
        <option value= {{downtimeCategory.CategoryId}} ng-repeat="downtimeCategory in Model.DowntimeCategories">{{downtimeCategory.CategoryName}}</option>
        </select> 
 <button ng-click="Model.StartDowntimeEvent()">Start Downtime Event</button> 
         <button ng-click="Model.StopDowntime()">Stop Downtime Event</button>

我选择localStorage - link。

基本上,你是保存js对象在浏览器中的字符串,所以你可以得到的数据,只要你想要的,即使在10000刷新或5个月。这是存储这些类型数据的更优雅的方式,而不是cookie

数据以名称/值对的形式存储,网页只能访问自行存储的数据。

与cookie不同,存储限制要大得多(至少5MB)信息永远不会传输到服务器。