<!DOCTYPE html>
<html ng-app="myApp">
<head>
<title>Parse Expression Example</title>
<script type="text/javascript" src="js/angular.min.js"></script>
</head>
<body>
<div id="emailEditor" ng-controller="MyController">
<input ng-model="to" type="email" placeholder="Recipient">
<textarea ng-model="emailBody"></textarea>
<div id="emailPreview">
<pre>__previewText__</pre>
</div>
</div>
</body>
<script type="text/javascript">
angular.module('myApp', ['emailParser'])
.controller('MyController', ['$scope', 'EmailParser', function($scope, EmailParser){
$scope.to = 'clearbug666@gmail.com';
$scope.emailBody = 'Hello __to__';
$scope.$watch('to', function(newVal, oldVal, scope){
if(newVal !== oldVal){
$scope.$watch('emailBody', function(body){
if(body){
$scope.previewText = EmailParser.parse(body, {to: $scope.to});
}
});
}
});
$scope.$watch('emailBody', function(body){
if(body){
$scope.previewText = EmailParser.parse(body, {to: $scope.to});
}
});
}]);
angular.module('emailParser', [])
.config(['$interpolateProvider', function($interpolateProvider){
$interpolateProvider.startSymbol('__');
$interpolateProvider.endSymbol('__');
}])
.factory('EmailParser', ['$interpolate', function($interpolate){
return {
parse: function(text, context){
var template = $interpolate(text);
return template(context);
}
};
}]);
</script>
</html>