angularjs - Dynamically add angular attributes to an element from a directive -
I am trying to create a directive which is a slow Ajax calls, in fact it is the idea that "Button" -Loading "is to set up the attribute, and the instruction is to add the rest of the goods.
This is the html code:
& lt; Button class = "btn btn-primary" name = "commit" type = "submit" ng-loading = "loading signup" & gt; & Lt; / Button & gt; Submit
and this is the directive code:
. Replace: False, Terminal: True, Link: Function (Scope, Element, ethers) {element.attr}, 'Directive', ['$ compile', 'function' ('Ng-class', '{loading:' enters ['njyloding'] + '}'); Element.attr ('ng-disabled', attrs ['ngLoading']); element.append (angular element '& Lt; img class = "loading-icon" src = "/ assets / images / loading-icon .gif" />); $ compilations (element.contents ()) (scope);}}; }];
All this looks correct in the provided HTML, but the attributes added to the instructions are not really fun. I can take those attributes to the HTML code and everything works great But in some places it is some unnecessary code.
I referenced the post but it does not solve my problem.
Any comments / suggestions are welcome Thanks in advance.
You do not have to compile that instruction again if you want to have some DOM manipulation, You can add or remove classes in relation to changes of a scope property, you can use $ watch instead.
JAVASCRIPT
.directive ('ngLoading', function () {return function (scope, element, attrs) {var img = angular .element ('lt; img class = "loading-icon" src = "/ assets / images / loading-icon.gif" />'); element.append (IMG); scope $ watch (attrs.ng loading) , Function (loading) {if (hodeling) {img.removeClass ('ng-hide'); element.addClass ('loading'); element.attr ('disabled', '');} Else {img Element.removeClass; element.removeAttr ('disabled');}}}}};});
> Note : Your code does not work because it gathers elements of elements, not elements, where you associate those attributes / P>Try
$ compile (elem) (scope);
and it should work properly, but I do not recommend it because every instruction is such that Instructions will be re-compiled with the instructions.UPDATE : Before using the
$ compile
element to prevent infinite compilation, 'Remove it.elem.removeAttr ('ng loading'); $ Collection (ELEM) (scope);
Comments
Post a Comment