gpt4 book ai didi

javascript - 值更改时 Angular ng-bind 未更新

转载 作者:行者123 更新时间:2023-11-30 16:53:54 25 4
gpt4 key购买 nike

我想绑定(bind)一个 span 元素的内容(即元素的当前位置)。

问题:当我更改元素位置时,angular 不会更新 ng-bind 属性的值。

这是我的 html:

!doctype html>
<html lang="en" ng-app="exempleApp">
<head>
<meta charset="utf-8">
<title>jQuery UI Draggable - Default functionality</title>
<link rel="stylesheet" href="jquery-ui.css">
<script src="jquery2.1.4.js"></script>
<script src="jquery-ui.js"></script>
<script type="text/javascript" src="angular.min.js"></script>
<script type="text/javascript" src="exempleApp.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css">
<style>
.movable { width: 150px; height: 150px; padding: 0.5em; background- color: green;}
</style>


<script>
$(function() {
$( ".movable" ).draggable(
{
drag: function(){
var offset = $(this).offset();
var xPos = offset.left;
var yPos = offset.top;
var thisId = $(this).attr('id');
$('#' + thisId + ' .posX').text(xPos);
$('#' + thisId + ' .posY').text(yPos);
}
}
);
});
</script>

</head>
<body ng-controller="imgController as ctrl">

<div id="1" class="ui-widget-content, movable" >
<p>Drag me around</p>
<span class="posX" ng-bind="ctrl.presentation.images[0].posX"></span>
<span class="posY" ng-bind="ctrl.presentation.images[0].posY"></span>
</div>

<div id="2" class="ui-widget-content, movable" >
<p>Drag me around</p>
<span class="posX" ng-bind="ctrl.presentation.images[1].posX"></span>
<span class="posY" ng-bind="ctrl.presentation.images[1].posY"></span>
</div>

<div >
<span ng-bind="ctrl.presentation.images[0].posX"></span>
<span ng-bind="ctrl.presentation.images[0].posY"></span>
</div>

</body>
</html>

这是我的 exempleApp.js:

(function() {
var app = angular.module("exempleApp", []);

app.controller('imgController', function(){
this.presentation = pres;
});

var pres = {

images: [
{
posX: "0",
posY: "0"
},
{
posX: "0",
posY: "0"
}
]
};

})();

感谢帮助

最佳答案

https://docs.angularjs.org/api/ng/directive/ngBind

Typically, you don't use ngBind directly, but instead you use the double curly markup like {{ expression }} which is similar but less verbose.

试试这个:

<span class="posX">{{ctrl.presentation.images[1].posX}}</span>

完整解释 Databinding in AngularJS

关于javascript - 值更改时 Angular ng-bind 未更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30075757/

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