1

I have "ugly" button which looks like this (I need to do it in this way, without ng-disabled):

<button uib-tooltip="some text" ng-class="!result.checkStatus === true ? 'disabled' : ''"  ng-click="!result.checkStatus  === true ? '' : DoSomething(result)"> </button>

If !result.checkStatus === true then button is disabled and ng-click doesn't work. In other case is enabled and ng-click activates function DoSomething(result) from the controller.

Everything works fine. But i'd like to do directive for this. How can I do this?

This is what I did already:

<button uib-tooltip="some text" my-own-directiv func="DoSomething(result)" param="result.checkStatus"> </button> 

Directive:

angular.module("mod").directive("myOwnDirectiv", function () {
    return {
        restrict: "AE",
        scope: {
            param: "=",
            func: "&"
        },
        template: " ng-class='!{{param}} === true ? 'disabled' : '' '  ng-click='!{{param}}  === true ? '' : func()' "

    };
});

I know that it won't work but I just wanted to show what I mean.

1
  • You can not use template here. Because the template will be rendered inside the element you applied the attribute directive to. Commented Aug 25, 2016 at 9:41

1 Answer 1

3

Please check: JSFiddle

Create your directive like:

angular.module('Joy', [])
  .controller("JoyCtrl", ['$scope', function($scope) {
    $scope.isActive = true;
    $scope.myHandler = function() {
      alert('myHandler is invoked');
    }
  }])
  .directive('myButton', function() {
    return {
      scope: {
        active: '=',
        handler: '&'
      },
      template: '<button ng-class="{disabled: !active}" ng-click="onClick()">Click me</div>',
      controller: ['$scope', function($scope) {
        $scope.onClick = function() {
          if ($scope.active) {
            $scope.handler();
          }
        };
      }]
    };
  })

And use it like:

<div ng-app="Joy" ng-controller="JoyCtrl">
  Active:
  <my-button active="isActive" handler="myHandler()"></my-button>
  <br> Disabled:
  <my-button active="!isActive" handler="myHandler()"></my-button>
</div>

Update 1

Here is the updated version with tooltip: https://jsfiddle.net/asahcmz5/2/

Update 2

How to pass class into directive: https://jsfiddle.net/asahcmz5/4/

Sign up to request clarification or add additional context in comments.

3 Comments

But tooltip won't work there uib-tooltip="some text"
It is not the problem related to the directive definition itself. It should be another question. But here you go: JSFiddle with tooltip
Now I think template it's a wrong approach for me. Because if I would add anything e.g. class=" " then I have to add it in directive. I'd like to use it on existing button, in this way: <button class="btn btn-default" active="!isActive" handler="myHandler()"></button>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.