Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change the function of an ng-click based on other events?

I have a problem, what I am trying to do is:

I have this button, if the user is unsubscribed, he can click the button to get subscribed. Now the thing is, if the user is subscribed I want him to click the button to get unsubscribed.

<button class="btn btn-primary subscribe-btn" ng-click="subscribe()" >
    Subscribe
</button>

How can I do this in angularJS? Based on the state of the user (eg: isSubscribed=true) change what the button does, how it looks and what function does it call..

like image 305
Rus Paul Avatar asked Jul 22 '14 08:07

Rus Paul


People also ask

Can we put condition in NG-click?

We can add ng-click event conditionally without using disabled class.

Can we call two functions on Ng-click?

When an HTML is clicked, the ng-click directive tells the AngularJS script what to do. In this article, we will learn how to get many/multiple functions to the ng-click directive passed, in just one click. The key is to add a semi-colon (;) or a comma (,). All the functions must be separated by a (;) or a (, ).

Can we use NG-click and Onclick together?

For a single btn, it's ok to use ng-click or onclick in the ng-app . There is no difference between the two functions. For effective team work, you,d better to have an account with each other. In Angular apps, ng-click is recommended.

What is the difference between click and Ng-click?

Another significant difference between ng-click and onclick is the execution context. Code inside an onclick attribute executes against the global window object, while an expression inside of ng-click executes against a specific scope object, typically the scope object representing the model for the current controller.


2 Answers

Try this:

<button class="btn btn-primary subscribe-btn" ng-click="isSubscribed ? unsubscribe() : subscribe()" >
    <span ng-show="isSubscribed">Subscribe</span><span ng-hide="isSubscribed">Unubscribe</span>
</button>

This will call the correct function, and display the correct text, depending on isSubscribed.

However, a cleaner option would be to handle this in the controller, resulting in something like this HTML:

<button class="btn btn-primary subscribe-btn"
        ng-controller="ButtonController"
        ng-click="toggleSubscribe()" >
    {{subscribeButton}}
</button>

Just set the button's text depending on whether or not the user is subscribed:

app.controller('ButtonController', ['$scope', function($scope) {
    $scope.subscribed = false;
    $scope.subscribeButton = $scope.subscribed ? 'Unsubscribe' : 'Subscribe';
    $scope.toggleSubscribe= function() {
        $scope.subscribed = !$scope.subscribed; // Handle subscription...
    };
}]);

Now, even better would be to use a template by creating a custom element directive:

app.directive('subscribeButton', function() {
    return {
        templateUrl: 'subscribe-button.html'
    };
});
like image 166
Cerbrus Avatar answered Oct 04 '22 11:10

Cerbrus


for button you could have :

<button ng-init="subscribed = true" class="btn btn-primary subscribe-btn" ng-click="changeSubscription()" >
    {{button}}
</button>

And in your app.controller you could have

$scope.changeSubscription() = function(){
    if($scope.subscribed){
        $scope.button = "subscribe";
        $scope.subscribe = false;
        unsubscribe();
    }else{
        $scope.button = "unsubscribe";
        $scope.subscribe = true;
        subscribe();
    }
}
like image 34
Lrrr Avatar answered Oct 04 '22 10:10

Lrrr