Skip to content

Instantly share code, notes, and snippets.

@cjlyth
Last active August 29, 2015 14:02
Show Gist options
  • Save cjlyth/c2a33dd277eb35f1fc3f to your computer and use it in GitHub Desktop.
Save cjlyth/c2a33dd277eb35f1fc3f to your computer and use it in GitHub Desktop.
angular directive that sync's a model to a scope var using a custom control
<pre>{{myObj | json}}</pre>
<pre>{{myProperty | json}}</pre>
<input ng-model="myObj.myVal" type="text" ></test>
<input ng-model="myObjVal" type="text" ></test>
<div bind-property="myObjVal" ng-model="myObj.myVal"></div>
'use strict';
/* Directives */
angular.module('app.directives', [])
.directive('bindProperty', [function () {
return {
restrict: 'A',
require: '^ngModel',
link: function (scope, iElement, iAttrs, ngModel) {
var pName = iAttrs.bindProperty
, oName = iAttrs.ngModel;
ngModel.$render = function() {
scope[pName] = ngModel.$viewValue;
};
scope.$watch(pName, function(n, o){
ngModel.$setViewValue(scope.$eval(pName));
});
}
};
}]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment