Last active
August 29, 2015 14:11
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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