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

标签 javascript jquery html angularjs

我想绑定(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/

相关文章:

html - 如何删除表格中行和列之间不需要的空间?

javascript - AngularJS 上 'global' 变量的使用

jquery 执行追加/前置,但在中间,如 midpend,或指定挂起位置?

php - 动态绑定(bind)Jquery回调函数

javascript - jquery 手机 : multiple data-filters showing

java - 从 html 行中提取 href 的正确正则表达式是什么? ( java )

javascript - 嵌套元素中的文本替换

javascript - Jest( react 测试工具)错误 - 解析错误 : Line 1: Illegal import declaration

javascript - 使用 javascript 的元素转换

javascript - 如何使用 Javascript 将 csv 格式化为 html 表格行和列