Skip to content

Instantly share code, notes, and snippets.

@bignimbus
Created March 30, 2016 18:15
Show Gist options
  • Save bignimbus/e2609bacb1caaf0d57197c593058ac45 to your computer and use it in GitHub Desktop.
Save bignimbus/e2609bacb1caaf0d57197c593058ac45 to your computer and use it in GitHub Desktop.
Inheriting ngModel in an Angular directive
angular.module('myModule', [])
.directive('myDirective', myDirective);
function myDirective () {
return {
restrict: 'E',
scope: {
inputName: '@',
model: '='
},
template: '<section>'
+ '<input type="text" name="{{inputName}}" ng-model="model[inputName]"></input>'
+ '</section>'
};
}
<form>
<input type="text" name="bar" ng-model="foo.bar"></input>
<input type="text" name="baz" ng-model="foo.baz"></input>
<my-directive model="foo" input-name="bing">
<!-- foo.bing is bound to the input in the my-directive template -->
</form>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment