Last active
August 29, 2015 14:02
-
-
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
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
<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> |
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
'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