簡體   English   中英

在AngularJS中按F5后,如何防止選中的選項和文本框被清除,直到單擊按鈕

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

我有一個選擇選項和一個文本框,當用戶選擇一個選項並在文本框內輸入文本時,數據將保存在sql數據庫中。我需要知道如何防止用戶按下時清除所選選項和文本框內的文本F5,直到在AngularJS中單擊一個按鈕,才可以清除選擇選項和文本框。這是我嘗試過的方法,但它不起作用。

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),信息永遠不會傳輸到服務器。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM