Giter VIP home page Giter VIP logo

angular-placeholder's Introduction

Angular Placeholder Directive

This directive will shim in placeholder support for browsers that do not support placeholders. The directive may also make styling easier by letting you combine your rules. It does not use a jQuery plugin and simulates the behavior by adding text for input and textarea elements when they do not have focus.

npm version Build Status Dependencies Dev Dependencies

Demo

There's a live demo courtesy of Plunker. In case you want some more advanced styling, there's another demo that helps illustrate what you can do.

Browser Support

Placeholder styling was natively implemented in IE 10, Chrome 4, Firefox 4, Safari 5, Opera 11.6. Internet Explorer 8 and 9 do not support this natively, but many places are forced to still code for those browsers.

How to Use

Using this plugin needs only three easy steps.

  1. Include the plugin into your page.

    <script src="tai-placeholder.js"></script>
    
  2. Add this somewhere to the module dependencies for your Angular application.

    angular.module('yourApplication', [ 'taiPlaceholder' ]);
    
  3. Make sure your element has both "ng-model" and "placeholder" attributes.

    <input type="text" ng-model="firstName" placeholder="First Name" />
    
    <textarea ng-model="feedback" placeholder="Provide feedback!"></textarea>
    
  4. Style the placeholder with some CSS for IE8 and IE9.

    .placeholder {
        color: #aaa;
    }
    

Further Configuration

There is an extra attribute that you can specify on your input tags to control the name of the class that is applied when the input should be showing placeholder text. The default value for placeholder-class is "placeholder" but you can change it.

<input type="text" ng-model="firstName" placeholder="First Name"
    placeholder-class="placeholder-blue">
<input type="text" ng-model="lastName" placeholder="Last Name"
    placeholder-class="placeholder-red">

The above sample will apply different classes for the first name and the last name. Using this mechanism you can style the inputs differently. You can achieve the same effect by having your inputs use classes - the above is just one path to a goal.

To change the color of the text you still need to write some duplicate CSS. This is what's needed to change the color to red for all browsers.

.placeholder { color: red; }
::-webkit-input-placeholder { color: red; }
:-moz-placeholder { color: red; }
::moz-placeholder { color: red; }
:-ms-input-placeholder { color: red; }

The rules need to be split out because browsers must ignore rules that contain invalid CSS selectors, and the browsers don't understand other browser-specific pseudoclasses and pseudoelements.

Here's a breakdown of the selectors.

  • .placeholder - All browsers get this class, but IE8 and IE9 can style the text when this class is applied to the elements.
  • ::-webkit-input-placeholder - WebKit and Blink (Safari, Chrome, Opera 15+)
  • :-moz-placeholder - Firefox 4 - 18
  • ::-moz-placeholder - Firefox 19+
  • :-ms-input-placeholder - IE 10

Developing

First, clone the repository. Then you need to run npm install to get most of the dependencies and npm run bower for the rest.

Testing

Tests are always included. You can run them with the following command. It runs the test suite with PhantomJS.

grunt test

If live feedback during development is what you would like, use this. It's also easier to run tests in many different browsers with this command - just open up the server with many different browsers and the tests will run in all of them.

grunt watch

LICENSE

This plugin is licensed under an MIT license with an additional non-advertising clause. Read full license.

angular-placeholder's People

Contributors

basster avatar fidian avatar oliverbenns avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

angular-placeholder's Issues

Placeholder does not play well with required in 1.3 and ng-required in 1.2

In 1.3 I'd expect a required error key to be set when the input initially loads, both when using required and ng-required. Only when you type in a character and remove it will the required error key be set:
http://jsfiddle.net/eL55yebe/1/

In 1.2, using required works fine, but ng-required doesn't set the required error key:
http://jsfiddle.net/at4jkx46/

Note: in all cases, removing the placeholder directive allows the required error key to be set on load.

Please, cut a new release with latest bower support

Great plug-in. I tried quite a few angular placeholder shims and encountered various problems with each, but yours fit my needs exactly. One request, though: the latest release in the bower registry is a bit out of date.
Could you please release the latest version? I would prefer not to reference the current version by git sha.
Regards,
---Tim---

multiline placeholder

It would be good if multiline placeholder was supported for textarea with \n or something

Change NPM main file

It would be interresting that the angular modules export its name for easy use in webpack.

I would be as simple as creating a index.js file with

require('./lib/tai-placeholder');
module.exports = 'taiPlaceholder';

and use this as main in package.json

The difference is the usage of module.exports with the name of the ng module.

See for example what the angular-animate package looks like.

.

.

IE9 delete & tab out changes input value

In IE9, on the demo page, follow these steps:

  1. Click in First Name (or any of the fields)
  2. Type anything (I used a space)
  3. Use backspace or delete to clear out the field
  4. Press TAB to exit the field
  5. The placeholder text will now be the field's actual value

If you click out of the field, this doesn't happen.

NPM publish

Hello

can you publish version 1.0.3 on npm ?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.