angularjs - Detect if a function is bounded to the scope with & -
I have a directive popup
in which it has a separate scope with a function bond:
Template
& lt; Popups & gt; & Lt; Div class = "confirm-button" ng-show = "showConfirmbutton" ng-click = "on confirm ()" & gt; Confirmation & lt; / Div & gt; & Lt; / Popup & gt;
Director & Controller
Angular Modules ('app.directives'). Instructions ('popup', [function] {"strict use"; return {restricted: 'e', scope: {onConfirm: "& amp;"}, administrator: 'popup Ctrl', templateUrl: "partial / instructions / Popup.html "};}]); Angular.module ('app.controllers') controller (' popup ctrl ', [function ($ scope) {"strict use."; $ Scope.showConfirmButton = typeof $ scope.onConfirm === "function'; // Always correct: (// ...}]);
Usage
& popup onConfirm = "doSomething () "& Gt; & lt; / popup & gt;
What do I want to do: If a function is not bound to end-configure, then I confirm I want to hide the button.
This does not work, because $ scope.on type type. Confirm on === 'Function';
is always correct. If I inspect my area in order, I think that assesses $ scope (.onConfirm.toString)
- regardless of Whether or not I have a bond function:
"function (locals) {return parentGet (scope, local people);}" edit Anyone knows a way to find out if some functions & amp; Is it bound to confirm or not?
Its a planner (simplified):
You can add the linking function to the directive and check for the attribute value whether something is assigned: -
link: function (scope, elm), etr ) {Scope.showConfirmButton = !! Attr.on Confirm; },
Also keep in mind that your separate scope has defined the attribute as onConfirm
, so you can call it on-confirm
Need to be used as. I.e
& lt; Popup on-strength = "doSomething ()" & gt; & Lt; / Popup & gt; As far as I know, because they are going internally into the scope chain. Or as a hack you can see in the field of parents:
.directive ('popup return (restricted: "e", scope: { OnConfirm: "& amp;", Controller: 'popup-ctrl', link: function ('$ parse', function ($ paras) scope, elm, attr) {var elm = $ parse (attr.onConfirm) (scope) $ Parent); scope.showConfirmButton = !! attr.onConfirm & hasFunction (attr.onConfirm); function hasFunction (function) {return angular.isFunction ($ parse (func.substring (0, func. IndexOf ('('))) (scope $ parent));}} template: ''
Comments
Post a Comment