简体   繁体   English

引导模态中的自动完成

[英]autocomplete in bootstrap modal

I want to show the last input data filled by user in bootstrap modal, I tried using HTML autocomplete="on" attribute but failed, like done in this fiddle. 我想显示用户在引导程序模式下填充的最后一个输入数据,我尝试使用HTML autocomplete =“ on”属性,但失败了,就像这个小提琴一样。

Once user click on submit, on the second time it shows the hints according to previous filled input values. 用户单击“提交”后,第二次将根据先前填充的输入值显示提示。

http://bootsnipp.com/snippets/featured/login-form-in-a-modal

http://jsfiddle.net/KyleMit/0fscmf3L/

Now I'm using jQuery autocomplete method but in that we have to pass an array as source. 现在,我使用的是jQuery自动完成方法,但是我们必须将数组作为源传递。 See following example 请参阅以下示例

https://jqueryui.com/autocomplete/ https://jqueryui.com/autocomplete/

Suppose I save the text input using ng-model or something and save data in $rootScope or scope and then I refresh the browser, then scope will get vanish. 假设我使用ng-model或其他内容保存文本输入并将数据保存在$ rootScope或scope中,然后刷新浏览器,然后scope将消失。

Suppose i save the text input using ng-model or something and save data in $rootScope or scope and then i refresh the browser, then scope will get vanish. 假设我使用ng-model或其他内容保存文本输入并将数据保存在$ rootScope或scope中,然后刷新浏览器,然后scope将消失。

As you have said yourself, the values will not persist throughout your application because you will need to store values either in a database or in session variable for example. 正如您自己所说的那样,这些值不会在整个应用程序中持久存在,因为您将需要将值存储在数据库或会话变量中。

Once you have done this you can then pass the array to your js code like below. 完成此操作后,您可以将数组传递给您的js代码,如下所示。

$(function () {
    var availableTags = GetTags(); // GetTags() would be the method that gets your saved inputs.
    $("#tags").autocomplete({
        source: availableTags
    });
});

Code Updated I suppose that you are using the second approach, ie using angular js: We could store the user input to the browser cookie and read from it. 更新的代码我想您正在使用第二种方法,即使用angular js:我们可以将用户输入存储到浏览器cookie中并从中读取。 Below is documentation for using angular cookie: ( https://docs.angularjs.org/api/ngCookies/service/ $cookies) 以下是使用角度cookie的文档:( https://docs.angularjs.org/api/ngCookies/service/ $ cookies)

Below is the updated code: 下面是更新的代码:

var myapp = angular.module('myApp', ['ngCookies']);
var PageController = function($scope, $cookieStore) {
$scope.cookieList = $cookieStore.get('textName') || [];
$scope.storeDataToCookie = "";
$scope.totalCookie = 0;
$scope.getCookieList = function() {
    var arr = [];
    arr = $cookieStore.get("textName");
    $scope.cookieList = arr;
    $scope.totalCookie = $cookieStore.get("textName").length;
};
$scope.storeCookieData = function() {
    var array = $cookieStore.get('textName');
    if (array == null) {
        array = [];
    }
    array.push($scope.storeDataToCookie);
    $cookieStore.put('textName', array);
    $scope.cookieList = array;
    $scope.totalCookie = $cookieStore.get("textName").length;
};
$scope.deleteCookieData = function() {
    $cookieStore.remove('textName');
    $scope.cookieList = $cookieStore.get('textName') || [];
    $scope.totalCookie = 0;
};
$scope.retrieveCookieData = function() {
    //
    $scope.totalCookie = $cookieStore.get("textName").length;
};
}
myapp.controller('CookieTestController', PageController);

Html: HTML:

<div ng-app="myApp">
    <div data-ng-controller="CookieTestController">
        <input type="text" ng-model="storeDataToCookie" />
        <input type="button" ng-click="storeCookieData()" value="Save Cookie Data" />
        <input type="button" ng-click="retrieveCookieData()" value="Retrieve Cookie Data" />
        <input type="button" ng-click="deleteCookieData()" value="Delete Cookie Data" />
        <input type="button" ng-click="getCookieList()" value="List Cookies" />
        <br />
        <span>Total Cookies:<strong>{{totalCookie}}</strong></span>
        <p>List of cookie values:</p>
        <ul>
            <li ng-repeat="c in cookieList">
                {{c}}
            </li>
        </ul>
    </div>
</div>

Below is the Plunker: Plunker 以下是柱塞: 柱塞

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

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