Oops, guys. I'm developing custom directives here, and they need to have flexibly added properties (like an ng-class). I was able to develop the mechanism to do this, however, I came across a bizarre problem: it adds the property but it does not have any effect
I'm testing with angular-input-masks and ui-mask . My directive correctly adds the attributes, but they do not take effect.
Particularly, I think this is some kind of script time problem or something. What reinforces my thinking is that when I add the attributes directly in the html template, it works.
I've tried using $scope.$apply()
, but it worked bad: (.
Test: link
Thank you in advance.
Dynamic Property Policy
app.directive('mdiAttrs', function() {
return {
restrict: 'A',
replace: true,
link: (scope, elem) => {
let attrs = scope.mdiCustomProperties || {};
Object.keys(attrs).forEach(attrKey => elem.attr(attrKey, attrs[attrKey]));
}
};
});
Input directive
app.directive('mdiInput', () => {
return {
restrict: 'E',
replace: true,
templateUrl: 'templates/mdi-input.html',
scope: (() => {
const inputScope = angular.copy(directiveScopeModel);
inputScope.mdiType = '@?';
inputScope.mdiPlaceholder = '@?';
inputScope.mdiTitle = '@?';
return inputScope;
})(),
controller: $scope => {
$scope.isLoading = false;
$scope.mdiType = $scope.mdiType || 'text';
}
}
});
Template
<label class="mdi-directive">
<span class="mdi-label">{{mdiLabel}}</span>
<input type="{{mdiType}}"
class="mdi-component"
ng-model="mdiModel"
ng-click="mdiClick(this)"
ng-blur="mdiBlur(this)"
ng-class="{'loading': isLoading}"
ng-disabled="isLoading"
placeholder="{{mdiPlaceholder}}"
ng-required="mdiRequired"
title="{{mdiTitle}}"
mdi-attrs="mdiCustomProperties">
</label>
Policy call
<mdi-input mdi-model="mdiInputModel"
mdi-blur="doRequest"
mdi-label="MDI Input Directive"
mdi-custom-properties="{'ui-mask': '(999) 999-9999'}"
mdi-required="true">
</mdi-input>