Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/469.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/24.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范围。$watch btn触发器_Javascript_Angularjs - Fatal编程技术网

Javascript AngularJS范围。$watch btn触发器

Javascript AngularJS范围。$watch btn触发器,javascript,angularjs,Javascript,Angularjs,我有一个类似的应用程序: angular.module('slots', []).directive('slot', function () { var spin = { fn: { go: function (scope) { //many code and functions here(animation for 3000ms) //i will just use setTime

我有一个类似的应用程序:

angular.module('slots', []).directive('slot', function () {
    var spin = {
        fn: {
            go: function (scope) {
                //many code and functions here(animation for 3000ms) 
                //i will just use setTimeout for example
                setTimeout(function() {
                scope.btnTrigger = false;
                }, 3000)
            },
            link: function (scope, ele, attrs) {
                if (attrs.trigger && attrs.trigger !== undefined) {
                    if (scope[attrs.trigger] !== undefined) {
                        scope.$watch(attrs.trigger, function (newValue, oldValue) {
                            if (newValue) {
                                spin.fn.go(scope);
                            }
                        });
                    }
                }
            }
        };
    return spin;
});

var myApp = angular.module('myApp',['slots']);
myApp.controller('MyCtrl', function ($scope, $timeout) {
    $scope.btnTrigger = false;
    $scope.btnClick = function () {
        if($scope.btnTrigger == false){
        $scope.btnTrigger = true;
        }
    };
});

问题是按钮在第一次单击后将不起作用。如果我将scope.btnTrigger=false设置为;就在spin.fn.go()函数调用下面。但我真正想要的是,该按钮仅在动画完成后可用(例如3000毫秒后)。

问题在于指令中的go()函数。使用$timeout而不是setTimeout()。您将在特定超时后更新作用域,因此angular不知道您已经更新了作用域。但是,如果使用$timeout,angular将负责调用Scope.$apply(),在这里它将监视作用域的变化并相应地更新UI

这应该是更新后的go()函数

go: function (scope) {
            //many code and functions here(animation for 3000ms) 
            $timeout(function() {
            scope.btnTrigger = false;
            }, 3000);
        }

if(scope.btnTrigger==false){
应该会引发错误,因为作用域未定义。您可能是指键入错误的
$scope.btnTrigger
@nubinub,如果我使用jquery animate()oncomplete调用另一个函数怎么办?例如:elem.animate({“页边空白”:“-100px”},{持续时间:3000,'EaseInOut',complete:function(){scope.btnTrigger=false;}}}然后在这种情况下,您可以编写complete:function(){scope.btnTrigger=false;}apply(function(){scope.btnTrigger=false;});}scope.$apply(function(){scope.btnTrigger=false;});有效。我应该在文档中发现,如果您可以使用$timeout来实现这一点,请严格使用它,或者如果您使用的是第三方库,如上文所述,请使用scope.apply()。Angular docs建议除非需要,否则不要不必要地使用$apply()。