当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - 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

网友答案:

Maintain a variable isClicked and handle the callback accordingly.

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

    ...

}
网友答案:

Just create an array to solve this

var loadedIds = [];
$scope.ViewItems = function ($id) {
    var index = loadedIds.indexOf($id);
    if(index == -1){
        loadedIds.push($id);
        -------
    }
};
分享给朋友:
您可能感兴趣的文章:
随机阅读: