setting model properties from a directive
HTML
<script src="http://code.angularjs.org/1.0.0/angular-1.0.0.min.js"></script>
<div ng-app='myApp'>
<div test ng-model="thisModel" set-to="This Value"></div>
<div test ng-model="thatModel" set-to="That Value"></div>
<hr>
model: {{thisModel}}
model2: {{thatModel}}
</div>
JavaScript
var myApp = angular.module('myApp',[])
.directive("test", function() {
return {
template: '<button ng-click="setValue()">Set value</button>',
//scope: { 'ngModel': '@' }, // If I add a scope then ngModel doesn't get exposed at all
require: 'ngModel',
link: function(scope, iElement, iAttrs, ngModel) {
scope.setValue = function(){
ngModel.$setViewValue(iAttrs.setTo);
}
}
};
});