AngularJS - Directive's Class Name Cannot Bring Into Inner Template
Solution 1:
With the class
attribute, the element's class is set by JavaScript. With the ng-class directive, the class is set by the AngularJS framework. When there are more that one directive on an element, there is no guarantee of the order of execution of the code of the respective directives.
Avoid having AngularJS manipulate the DOM and having subsequent AngularJS manipulate the model based on the state of the DOM. With MVC frameworks the Model should be the single source of truth and the DOM should be directly determined by the Model.
<tit-txt inner-class="true ? 'editable' : ''" my-model="mdEnt.phone">
</tit-txt>
app.directive('titTxt', function () {
return {
restrict: 'E',
scope: {
title: '@',
fieldName: '@',
innerClass: '<',
myModel: '=',
},
link: function (scope, element, attrs) {
scope.$watch(attrs.innerClass, function(newValue) {
console.log("inner-class=", newValue);
});
},
template: `<div><span>{{title}}: </span>
<input id="{{fieldName}}" ng-class="innerClass"
name="{{fieldName}}" ng-model="myModel" />
</div>`,
};
})
Notice how the directive uses one-way, '<'
, binding to compute the value of the inner-class
attribute from an AngularJS Expression.
Also notice that I changed ng-model
to my-model
. The ng-
prefix is reserved for core AngularJS directives. Use of ng-model
should be specifically be avoided unless the custom directive properly integrates with the ngModelController.
Post a Comment for "AngularJS - Directive's Class Name Cannot Bring Into Inner Template"