如何在ng-click Angular上添加确认窗口

时间:2016-04-22 09:14:18

标签: angularjs angularjs-ng-click

我有一个ng-click链接。我需要点击确认窗口。

<h4><a href="" ng-click="makeUnfavourite(favourite.userId,$index);remove(favouriteData.data.result,$index)">Un-Favourite</a></h4>

目前它包括2个功能。我必须在确认后执行这两个功能。我的两个函数在usercontroller.js.plz中定义,见下面的代码

var userControllers = angular.module('userControllers', ['ui.bootstrap','gm']);
userControllers.controller('myProfileCtrl', function ($scope, $routeParams, $rootScope, $http, $location, $window, $timeout) {`  
$scope.makeUnfavourite=function(id,index){
    var indextoremove=index;
    var currentuserid=2;
    var favUserId=id;
    console.log(favUserId);
    var params = {
        currentuserid:2,
        favUserId:id
        };
       if(favUserId){
        $http.post($rootScope.STATIC_URL + 'users/makeUnFavourite', params).success(function(response){
            $scope.favHide=response;
            }).error(function(err){
            console.log("Error"+err);
        });
    }

};
$scope.remove = function(favourite,index){
    favourite.splice(index,1);
};
});

我必须执行makeUnfavourite()&amp;确认时删除()函数。我的角度更新鲜。现在我正在进行一个部分完成的项目

3 个答案:

答案 0 :(得分:1)

只需使用ng-confirm-click,就像这样:

<h4><a href="" confirmed-click="confirmedAction()" ng-confirm-click="Do you confirm?">Un-Favourite</a></h4>

DEMO

<强>更新: 对于角度指令,请参阅here ...

更新2 : 我知道了。你可以这样做:

$scope.confirmedAction = function() {
    makeUnfavourite(favourite.userId, $index);
    remove(favouriteData.data.result, $index);
};

答案 1 :(得分:0)

很容易,你有2个功能

@TargetApi(Build.VERSION_CODES.M)
public void onClickAlarmOn(View v) {

    final Calendar c = Calendar.getInstance();
    TimePicker alarm_time_picker = (TimePicker) findViewById(R.id.timePicker);
    int hour = alarm_time_picker.getHour(); //get selected hour
    int minute = alarm_time_picker.getMinute(); //get selected minute

    int hour_now = c.get(Calendar.HOUR); //get system's hour
    int minute_now = c.get(Calendar.MINUTE); //get system's minute

    int hour_result = hour - hour_now; //subtract the time selected by time.now of system
    int minute_result = minute - minute_now;

    String hour_result_string = String.valueOf(hour_result); //convert to string to display
    String minute_result_string = String.valueOf(minute_result);

    setToast_result("Alarm set to " + hour_result_string + " hours " + minute_result_string + " minutes");
}

现在你可以在makeUnFavorite中调用<a confirmed-click="makeUnfavourite(favourite.userId,$index)" ng-confirm-click="Confirm unfavorite?">Un-Favourite</a> 函数,或者在确认点击内调用,如下所示:

$scope.remove

我更喜欢第一种解决方案。完整代码:

控制器

<a confirmed-click="makeUnfavourite(favourite.userId,$index);remove(favouriteData.data.result,$index);" ng-confirm-click="Confirm unfavorite?">Un-Favourite</a>

HTML

var userControllers = angular.module('userControllers', ['ui.bootstrap','gm']);
userControllers.controller('myProfileCtrl', function ($scope, $routeParams, $rootScope, $http, $location, $window, $timeout) {`  
$scope.makeUnfavourite=function(id,index,favourite){
    var indextoremove=index;
    var currentuserid=2;
    var favUserId=id;
    console.log(favUserId);
    var params = {
        currentuserid:2,
        favUserId:id
        };
       if(favUserId){
        $http.post($rootScope.STATIC_URL + 'users/makeUnFavourite', params).success(function(response){
            $scope.favHide=response;
            //here we call the remove function, always inside the $http response for not having async issues
            $scope.remove(favourite,index);
            }).error(function(err){
            console.log("Error"+err);
        });
    }


};
$scope.remove = function(favourite,index){
    favourite.splice(index,1);
};
});

修改

还有一件事,如果你在$ http.post上使用这个结构会更好:

<a confirmed-click="makeUnfavourite(favourite.userId,$index,favouriteData.data.result)" ng-confirm-click="Confirm unfavorite?">Un-Favourite</a>

答案 2 :(得分:0)

ng点击返回确认100%有效

在html文件中调用delete_plot()函数

<i class="fa fa-trash delete-plot" ng-click="delete_plot()"></i> 

将此添加到您的控制器

 $scope.delete_plot = function(){
        check = confirm("Are you sure to delete this plot?")
        if(check){
            console.log("yes, OK pressed")
        }else{
            console.log("No, cancel pressed")

        }
    }