Reputation: 1195
In my angularjs project I need to go back and forth using both ng-src and src attributes in my tags.
Some images are static assets and will never change (they may be conditionally displayed) while other images are dynamic and dependent on scope variables.
May I use the mix of ng-src and src whenever I see fit?
I'm asking that because I once read that I should always use ng-src when working with angularjs, but I'm also afraid that I'm going to create bindings and watches that are really not necessary...
Upvotes: 2
Views: 838
Reputation: 32367
Actually the source code is pretty straightforward https://github.com/angular/angular.js/blob/v1.2.7/src/ng/directive/booleanAttrs.js
ng-src will always $observe your attributes.
// ng-src, ng-srcset, ng-href are interpolated
forEach(['src', 'srcset', 'href'], function(attrName) {
var normalized = directiveNormalize('ng-' + attrName);
ngAttributeAliasDirectives[normalized] = function() {
return {
priority: 99, // it needs to run after the attributes are interpolated
link: function(scope, element, attr) {
attr.$observe(normalized, function(value) {
if (!value)
return;
attr.$set(attrName, value);
// on IE, if "ng:src" directive declaration is used and "src" attribute doesn't exist
// then calling element.setAttribute('src', 'foo') doesn't do anything, so we need
// to set the property as well to achieve the desired effect.
// we use attr[attrName] value since $set can sanitize the url.
if (msie) element.prop(attrName, attr[attrName]);
});
}
};
};
});
As for $observe, from the documantation:
The observer function will be invoked once during the next
$digest
following compilation. The observer is then invoked whenever the interpolated value changes.
Or more Simple, if there is no interpolation there is no dirty checking. AFAIK, all performance issues angular.js has with (lots of) bindings is when it does dirty checking.
// no one registered attribute interpolation function, so lets call it manually
$watch(interpolateFn, function interpolateFnWatchAction(newValue, oldValue) {
Upvotes: 3