DMS-KH
DMS-KH

Reputation: 2787

how to disable more than one times when a link was click by user in Angularjs

I have using ViewItems() function in An-click='VewItems(id)' to call a method in Controller in Angularjs but it seem not good yet because when user click more time my images which load from Model it will get more image or element which I append to a div much as the amount of clicking.

Here is my Function

 $scope.ViewItems = function ($id) {

        $('.modal-title').html('');
        $('#bookimg').html('');
        // Fetch an item from book list
        if($id) {

            id = $id;
            $http({
                method: 'GET',
                url: url+id,
            }).then(function successCallback(response) {
                var http_code = response.data.s_respond;
                $('<div id="loading"></div>').appendTo('body');
                if (http_code === 200) {

                    SetErrors(http_code, 'OK');
                    var book_items = JSON.parse(response.data.data);
                    $('<h3>'+book_items.title+'</h3>').appendTo('.modal-title');
                    $('<img src=" '+book_items.image+' " width="100%" />').appendTo('#bookimg');
                    $('#myModal').modal({backdrop: 'static', keyboard: false});
                } else {

                    SetErrors(http_code, 'warning');
                }
            }, function errorCallback(response) {
                SetErrors(http_code, response.textStatus);
            });
        }
    };

HTML

Upvotes: 0

Views: 54

Answers (2)

byteC0de
byteC0de

Reputation: 5273

Just create an array to solve this

var loadedIds = [];
$scope.ViewItems = function ($id) {
    var index = loadedIds.indexOf($id);
    if(index == -1){
        loadedIds.push($id);
        -------
    }
};

Upvotes: 1

kvn
kvn

Reputation: 2300

Maintain a variable isClicked and handle the callback accordingly.

var isClicked = false;
$scope.ViewItems = function ($id) {
    if (isClicked) {
         return;
    }
    isClicked = !isClicked;

    ...

}

Upvotes: 2

Related Questions