0

I am using the datepicker from bootstrap-UI for angular. I would like to toggle between two functions. now() and clear(). Is this possible inside Angular's ng-click?

http://plnkr.co/edit/aMcKwXOSQwgnGwfNN9yI?p=preview

The toggle has the ng-click="today()" I would like to add clear() to it, thanks.

Controller Code:

      $scope.today = function() {
      $scope.dt = new Date();
      };
      $scope.today();
      $scope.clear = function () {
         $scope.dt = null;
      };

Edit - I saw this answer but I would like to explore all options.

3

1 Answer 1

3

You can create a toggle() function which will call the function you want based on a variable tracking the state. You can then simply use this function in your template as ng-click="toggle()".

Something like this.

var toggled = false;
$scope.toggle = function() {
    if (toggled) {
        $scope.clear();
    } else {
        $scope.today();
    }
    toggled = !toggled;
};

A shorter version

var toggled;
$scope.toggle = function() {
    $scope[toggled ? "clear" : "today"]();
    toggled = !toggled;
};

But if clear() and today() are no longer called in the template I would suggest taking them out of $scope.

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

Comments

Your Answer

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