Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/436.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在AngularJS中提交时,实现需要禁用的按钮的最佳方法是什么?_Javascript_Angularjs - Fatal编程技术网

Javascript 在AngularJS中提交时,实现需要禁用的按钮的最佳方法是什么?

Javascript 在AngularJS中提交时,实现需要禁用的按钮的最佳方法是什么?,javascript,angularjs,Javascript,Angularjs,我有一个带有提交按钮的表单,表单需要10秒钟才能返回,我不希望用户同时点击表单。到目前为止,我的方法是将按钮的文本设置为buttonText=“Loading…”,并使用ng disable,以及我在提交时设置的标志(isSubmitted)。考虑到这必须是一种通用模式,那么什么是最好的、最可重用的方法呢?我建议使用jQuery函数:记住“#”=控件id和“.”=css类 $("#create").click(function(){ $('body').append($(

我有一个带有提交按钮的表单,表单需要10秒钟才能返回,我不希望用户同时点击表单。到目前为止,我的方法是将按钮的文本设置为
buttonText=“Loading…”
,并使用
ng disable
,以及我在提交时设置的标志(
isSubmitted
)。考虑到这必须是一种通用模式,那么什么是最好的、最可重用的方法呢?

我建议使用jQuery函数:记住“#”=控件id和“.”=css类

    $("#create").click(function(){
        $('body').append($('<div id="test" class="btn">click me<div>'));
    });

    //-- if you create single button use this
    $("body").delegate("#test", "click", function() {
       alert("worked!"); 
     });

    //-- if you create many buttons use this
    // $("body").delegate(".btn", "click", function() {
      // alert("worked!"); 
    // });
$(“#创建”)。单击(函数(){
$('body')。追加($('click me'));
});
//--如果创建单个按钮,请使用此按钮
$(“body”).delegate(“#测试”,“单击”,函数(){
警惕(“工作!”);
});
//--如果您创建了许多按钮,请使用此按钮
//$(“body”).delegate(“.btn”,“click”,function(){
//警惕(“工作!”);
// });

使用自定义指令创建可重用组件。指令应创建一个隔离作用域,并使用“&”语法指定单击按钮时要调用的父作用域函数。传递回调函数,以便该指令可以在任务完成时撤消按钮标签更改和禁用属性

HTML:


如果
doStuff
来自元素的属性,您将如何执行此操作。理想情况下,
将调用
customFn
?您仍然需要指定回调参数:
on click=“customFn(cbFn)”
--。
<button wait-button do-stuff="doStuff(cbFn)">button label</button>
myApp.directive('waitButton', function() {
    return {
        scope: {
            doStuff: '&'
        },
        link: function(scope, element) {
            var normalText = element.text();
            var callbackFn = function() {
                console.log('callback')
                // element[0] is the (unwrapped) DOM element
                element[0].disabled = false;
                element.text(normalText);
            }
            element.bind('click', function() {
                element[0].disabled = true;
                element.text('Loading...');
                // Weird syntax below!  Arguments must 
                // be passed inside an object:
                scope.doStuff({cbFn: callbackFn});
            })
        }
    }
})

function MyCtrl($scope, $timeout) {
    $scope.doStuff = function(callbackFn) {
        console.log('doStuff');
        // Do stuff here... then call the callback.
        // We'll simulate doing something with a timeout.
        $timeout(function() {
            callbackFn()
        }, 2000)
    }
}