Skip to content

Instantly share code, notes, and snippets.

@alonronin
Last active August 29, 2015 14:11
Show Gist options
  • Save alonronin/83847739d37cb2e95337 to your computer and use it in GitHub Desktop.
Save alonronin/83847739d37cb2e95337 to your computer and use it in GitHub Desktop.
Angular Two Way data-binding with Casting and Filters (http://jsbin.com/tepoleceli/8/edit?html,js,output)
<!DOCTYPE html>
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular.min.js"></script>
<script src="angular.binding.js"></script>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body ng-app="myApp">
<div ng-controller="myCtrl as ctrl">
<ul>
<li ng-repeat="ip in ctrl.items">{{ip}}</li>
</ul>
<textarea cols="30" rows="10" ng-model="ctrl.textItems" ng-model-options="{ updateOn: 'blur' }"></textarea>
</div>
</body>
</html>
angular.module('myApp', [])
.controller('myCtrl', function($scope, $filter){
var self = this;
self.items = [
'192.168.1.1',
'192.168.1.2',
'192.168.1.3',
'192.168.1.4',
]
Object.defineProperty(self, 'textItems',{
get: function(){
return $filter('toText')(self.items)
},
set: function(value){
self.items = $filter('fromText')(value);
}
})
})
.filter('toText', function(){
return function(input){
return input.join('\n');
}})
.filter('fromText', function(){
return function(input){
return input.split('\n');
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment