javascript - Extending ngClick for adding additional function -
hello there way extend inbuilt directive?
this want do.
for example directive template :
<div ng-click="condition && handleclick()" ng-transclude></div>
here condition whether perform click div or not when reused directive , make nested end-up this.
note : condition in directive not change dynamically 1 time value.
<div ng-click="condition && handleclick()"> <div ng-click="condition && handleclick()"> <div ng-click="condition && handleclick()"></div> </div> </div>
so problem if ng-click added div angular binds touchstart or click , others div, want avoid bindings, due bindings many unnecessary events added div not needed.
now want extend ng-click directive , need add condition check before binding events element, if condition false not bind event else if true bind event.
for fixing can write own directive ng-click-condition , perform same operation ng-click condition check if lead code duplication , maintainability issue, finding way reuse in-built directive.
if understand trying correctly, should package logic inside directive.
set scope of directive scope: {} (scope: true, may work well. long each directive gets it's own scope)
inside link function of directive:
element.bind('click', function() { if (condition) { //do awesome } })
or if want avoid bindings unless condition met, reverse binding. still put in directive:
if (some condition) { element.bind('click', function() { //do awesome }) }
binding click , touchstart: how bind 'touchstart' , 'click' events not respond both?
edit: still not quite sold on needing extend ng-click. how conditionally adding ng-click?
Comments
Post a Comment