AngularJS ng-init

The ng-init directive allows you to evaluate an expression in the current scope. This can be used to initialize model values.

Usage:

<div ng-init="test1 = someValue"></div>
<div ng-init="test1 = (someValue |someFilter)"></div>

Example

Initialize date using ng-init and date filter.

<div ng-app="myApp" ng-controller='MyCtrl'>
  <label>Date:</label>
  <input type="text" ng-model="dateVal" ng-init="dateVal=(Date() | date:'yyyy/MM/dd')">
<div/>

<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.3/angular.min.js"></script>
<script type="text/javascript">
  var module = angular.module("myApp", []);
  module.controller('MyCtrl', function($scope) {
    $scope.Date = function () {return new Date();}
  });
</script>
refresh

Share this article: share on Google+ share on facebook share on linkedin tweet this submit to reddit

Comments

Click here to write/view comments