Taking a look to ngRequired directive I found that they not use isolated scope, but they manage to observe the "required" attribute and check if there are changes (getting the final boolean expression result)
attr.$observe('required', function() {
validator(ctrl.$viewValue);
});
If in the controller scope I have a boolean var called isRequired I can directly write:
<input id="year"
type="text"
name="year"
data-ng-model="currentVehicleEdit.year"
data-ng-required="isRequired" />
and I get the expression evaluated to true or false.
I have tried the same approach on a directive (dropdown validation) but all I get is the text value for that attribute. So I have to enter something like this to get results ("true", "false"):
<select id="involvementType"
name="involvementType"
data-ng-model="currentVehicleEdit.involvementType"
data-ng-options="a.name for a in involvementTypes"
data-combocontainsinvalidselection="{{showForm}}"
>
</select>
If I try to use it in the same ways as in the ngRequired (data-combocontainsinvalidselection="showForm") I just got plain text ("showForm"), not the expression evaluated.
About the directive's code I have created:
mydirectives.directive('combocontainsinvalidselection', [
function () {
return {
require: "?ngModel",
link: function(scope, elm, attr, ctrl) {
if (!ctrl) return;
var validator = function (value) {
if (attr.combocontainsinvalidselection == true && value.id==0) {
ctrl.$setValidity('combocontainsinvalidselection', false);
return;
} else {
ctrl.$setValidity('combocontainsinvalidselection', true);
return value;
}
};
ctrl.$formatters.push(validator);
ctrl.$parsers.unshift(validator);
attr.$observe('combocontainsinvalidselection', function () {
validator(ctrl.$viewValue);
});
}
};
}
]);
How does the ng-required directive manage to get the expression evaluated?