javascript - angularjs form input suggestions - Stack Overflow

I have a problem with a form in angularjs.Example with classic html & php<form name="myForm

I have a problem with a form in angularjs.

Example with classic html & php

    <form name="myForm" action="post.php" method="post" autoplete="on">
        <input name="namename" type="text" />
        <input name="email" type="text" />

        <input name="submit" type="submit" value="submit" />
    </form>

which works as expected. On the second visit, after i submitted the form for the first time, i just need to type the first letter and the input field will suggest something based on the first post.

The same form in angular.

    <form name="myForm" ng-submit="test(user)" autoplete="on">
        <input name="name" type="text" ng-model="user.name" autoplete="given-name" />
        <input name="email" type="text" ng-model="user.email" />

        <input name="submit" type="submit" value="submit" />
    </form>

On the second visit here the form will suggest nothing at all, which is very irritating.

Is there any fix for that?

Example

thanks in advance.

I have a problem with a form in angularjs.

Example with classic html & php

    <form name="myForm" action="post.php" method="post" autoplete="on">
        <input name="namename" type="text" />
        <input name="email" type="text" />

        <input name="submit" type="submit" value="submit" />
    </form>

which works as expected. On the second visit, after i submitted the form for the first time, i just need to type the first letter and the input field will suggest something based on the first post.

The same form in angular.

    <form name="myForm" ng-submit="test(user)" autoplete="on">
        <input name="name" type="text" ng-model="user.name" autoplete="given-name" />
        <input name="email" type="text" ng-model="user.email" />

        <input name="submit" type="submit" value="submit" />
    </form>

On the second visit here the form will suggest nothing at all, which is very irritating.

Is there any fix for that?

Example

thanks in advance.

Share Improve this question edited Mar 24, 2014 at 13:11 arkhon asked Mar 23, 2014 at 11:48 arkhonarkhon 7652 gold badges12 silver badges28 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

That behaviour you're describing is done by the browser and is not guaranteed to work in all situations. It's actually quite easy to do in AngularJS; just keep track of a shared state object. This can be done in several ways, the most easiest by using the value provider like this:

// Create an injectable object with the name 'userInput'
angular.module('myApp').value('userInput', {});

Now inject this object in the controller that is handling the form like this:

angular.module('myApp').controller('MyController', function($scope, userInput) {
  // Assign the state object to the scope so it's available for our view
  $scope.user = userInput;
});

Render the form as you did and you'll see that the state of the form is kept. In fact, this is one of the hidden gems when programming with Angular since it allows you to keep very plex state information, which was previously pretty impractical.

Live demo can be found in this plunker.

Edit

One way to get the autoplete to work is to maintain datalist elements. Just store the previous entered values in an array and use a ng-repeat to render all the options. Associate the input element with the datalist using the list attribute and you'r good to go.

<input list="nameHistory" type="text" ng-model="user.name" />

<datalist id="nameHistory">
  <option ng-repeat="item in userHistory.name" value="{{ item }}"></option>
</datalist>

Live demo can be found in this plunker.

Just add to the input tag this attribute autoplete="off"

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744171877a4561575.html

相关推荐

  • javascript - angularjs form input suggestions - Stack Overflow

    I have a problem with a form in angularjs.Example with classic html & php<form name="myForm

    8天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信