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="foo.thisModel" set-to="This Value"></div>
<div test ng-model="foo.thatModel" set-to="That Value"></div>
<hr>
model: {{foo.thisModel}}
model2: {{foo.thatModel}}
</div>
JavaScript
var myApp = angular.module('myApp',[])
.run(function($rootScope) {
$rootScope.foo = {};
})
.directive("test", function($parse) {
return {
template: '<button ng-click="setValue()">Set value</button>',
scope: true,
require: 'ngModel',
link: function(scope, iElement, iAttrs, ngModel) {
scope.setValue = function(){
ngModel.$setViewValue(iAttrs.setTo);
}
}
};
});