I'm trying to build a directive around an input element that responds when the model is dirtied or touched. The required ngModel seems to reflect the changes in the value and view of the input model, but none of the other attributes.
I'm suspecting it has something to do with the fact that I'm including ng-model in two elements, but I haven't figured out how to use it just once.
Ideally, I would like something that is created like this:
<input test-directive label="'My Label'" type="text" ng-model="testObject.text"/>
And results in something like:
<label>
<div>My Label</div>
<input ng-model="testObject.text" ng-blur="input.focus=false" ng-focus="input.focus=true"/>
Focused: true (input.focus)
Pristine: false (ngModel.$pristine)
</label>
Here is what I have so far: fiddle
<div test-directive ng-model="testObject.text" l="'Test Input'" f="testObject.focus">
<input type="text" ng-model="testObject.text" ng-blur="testObject.focus=false" ng-focus="testObject.focus=true" />
</div>
The directive watches ngModel.
app.directive('testDirective', ['$compile',
function ($compile) {
'use strict';
return {
restrict: 'A',
require: "ngModel",
scope: {
l: '=',
f: '='
},
link: function (scope, element, attr, ngModel) {
var input = element.find('input');
scope.$watch(function () {
return ngModel;
}, function (modelView) {
scope.modelView = modelView
});
},
template:
'<div>' +
'<label>' +
'{{l}}' +
'<div class="iwc-input" ng-transclude></div>' +
'</label>' +
'focus: {{f}}' +
'<pre>{{modelView|json}}</pre>' +
'</div>',
transclude: true,
replace: false
};
}]);