我有類似這樣的應用程序:
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毫秒之後)。
'if(scope.btnTrigger == false){'應該引發一個錯誤,因爲scope是未定義的。你可能的意思是'$ scope.btnTrigger' – nubinub
@nubinub錯誤的輸入錯了 –