A tiny AngularJS directive to set ng-model (and consequently field value) based on form fields attributes.
1- Download plugin (bower install angular-initial-value
) or use CDN address and import it after AngularJS:
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js"></script>
<script src="//cdn.jsdelivr.net/angular.initial-value/latest/angular-initial-value.min.js"></script>
2- Load the module in your application:
var app = angular.module('myApp', ['initialValue']);
3- Add initial-value
attribute for your form fields, it supports the most common inputs like text
, email
, tel
, number
, color
, range
, search
, date
, url
, password
, checkbox
, radio
, select
, textarea
.
<body ng-app="myApp">
<input type="text" value="John" name="text" initial-value ng-model="text" id="text"/>
</body>
You can pass the start value to attribute as well (this feature is not available only for checkbox
and radio
)
<body ng-app="myApp">
<input type="text" name="text" initial-value="John" ng-model="text" id="text"/>
</body>
The script will read values from value
, checked
and selected
attributes and load them into its respectives ng-model
's.
Check CONTRIBUTING.md for more information.
After starting this project I found this another one which intends deal with the same issue.
This projected is licensed under the terms of the MIT license.