gpt4 book ai didi

javascript - 无法使用 AngularJS 移动元素

转载 作者:行者123 更新时间:2023-12-03 06:11:22 24 4
gpt4 key购买 nike

这是我的标记:

<button class="left" ng-mousedown="moveLeft()">Left</button>

这是我的代码:

angular.module('ionicApp', ['ionic'])

.controller("wallControl", function($scope) {

var leftTimer;

$scope.mousePress = function() {
moveLeft();
};

var moveLeft = function() {
if (circleX > 0) {
circleX -= 4;
}
leftInterval = $timeout(moveLeft, 1000);
};

$scope.mouseRelease = function() {
$timeout.cancel(leftTimer);
};
});

控制台中没有错误。为什么变量circleX没有改变?我以前从未使用过 Angular,所以我可能犯了一个愚蠢的错误。

最佳答案

一个问题是您正在调用 mouseLeft(),但该函数未在 $scope 上公开。

另一个问题是您从未向 leftTimer 分配任何内容。

另一个问题是您没有注入(inject)$timeout

我会尝试这样的事情:

<button class="left" ng-mousedown="mousePress()" ng-mouseup="mouseRelease()">Left</button>

angular.module('ionicApp', ['ionic'])

// notice the DI syntax (which is not only a best practice, but is necessary if you minify your code)
// notice $interval service is injected
.controller("wallControl", ['$scope', '$interval', function($scope, $interval) {

// used to store the $interval handle so it can be cancelled
var leftTimer;

$scope.mousePress = function() {
// start running moveLeft every 1000ms
leftTimer = $interval(moveLeft, 1000);
};

var moveLeft = function() {
if (circleX > 0) {
circleX -= 4;
}
// no need to call itself on $timeout - $interval doing that
};

$scope.mouseRelease = function() {
// stop the moveLeft interval
$interval.cancel(leftTimer);
};
}]);

更新:多方向

非常简单,只需对代码进行一些小更改即可。我更进一步详细阐述了几个其他概念,以使代码稍微更加实际。

<button class="left" ng-mousedown="startMoveX(-1)" ng-mouseup="stopMoveX()">Left</button>
<button class="right" ng-mousedown="startMoveX(1)" ng-mouseup="stopMoveX()">Right</button>

angular.module('ionicApp', ['ionic'])

.controller("wallControl", ['$scope', '$interval', function($scope, $interval) {
// some settings (magic numbers are bad m'kay)
var xSpeed = 4;
var xBounds = { min: 0, max: 1000 };

// circleX need to be defined somewhere
// how you will use it for drawing is another story
var circleX = (xBounds.min + xBounds.max) / 2.0;

// x-motion variables
var xMoveTimer;
var xDirection;

// starts the x-motion
$scope.startMoveX = function(direction) {
// track which direction to move (used as multiplier on speed)
xDirection = direction;

// make sure the xMoveTimer is not already running
if(!xMoveTimer){
// start running moveX every 1000ms
xMoveTimer = $interval(moveX, 1000);
}
};

// stops the x-motion
$scope.stopMoveX = function() {
if(xMoveTimer){
// stop it
$interval.cancel(xMoveTimer);

// release it
xMoveTimer = undefined;
}
};

// performs the x-motion
var moveX = function() {
// move it move it
circleX += xSpeed * xDirection;

// lock it to bounds
if(circleX < xBounds.min){
circleX = xBounds.min;
}
else if(circleX > xBounds.max){
circleX = xBounds.max;
}
};
}]);

关于javascript - 无法使用 AngularJS 移动元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39299928/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com