javascript - 具有双向绑定(bind)的隔离范围指令不反射(reflect) Controller 范围的变化

标签 javascript angularjs angularjs-directive

总结:

当尝试修改已在隔离作用域指令中为双向数据绑定(bind)设置的作用域变量(数组)时,对隔离作用域指令中变量的修改不会反射(reflect) Controller 作用域内的更改。

玩具示例:

目前我有一个排序指令,我想在使用 ng-repeat 的表上定义它。 ng-repeat 在 ATableController Controller 范围内使用相同的数据来创建表:

$scope.tableData = [{'col1' : 1,'col2' : 'fff'},
                             {'col1' : 2,'col2' : 'aaa'},
                             {'col1' : 3,'col2' : 'bbb'},
                             {'col1' : 4,'col2' : 'ccc'}];

我希望排序指令在其隔离范围内操作相同的数据。

在指令中设置如下

... scope :
                  {
                    tableSortData        : '=',
                    tableSortRowAccessor : '=',
                    tableSortPrimer      : '='
                  },
...

索引中引用如下

...
<body ng-controller="ATableController">
       <table table-sort table-sort-data="tableData">
...

在链接函数中,点击处理程序被分配给表头。单击此处理程序会触发 sort 函数,该函数最终会运行这行代码。

scope.tableSortData = [];//这将是真实代码中的排序行

这些更改不是是在 Controller 内进行的,因此表格不会按预期更改。

这是一个 vanilla js 问题,(正在制作错误的引用等...)还是我的 angular 指令有问题。无论哪种方式,我做错了什么,如何解决?

代码:

笨蛋

http://plnkr.co/edit/5yxRj6?p=info

索引.html

  <head>
    <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script data-require="angular.js@*" data-semver="1.2.0" src="http://code.angularjs.org/1.2.0/angular.js"></script>
    <link rel="stylesheet" href="style.css" />
    <script src="script.js"></script>
  </head>

  <body ng-controller="ATableController">
   <table table-sort table-sort-data="tableData">
    <thead>
        <tr>
            <th>Column1</th>
            <th>Column2</th>
        </tr>

    </thead>
    <tbody>
        <tr ng-repeat="row in tableData" >
            <td>{{row.col1}}</td>
            <td>{{row.col2}}</td>
        </tr>
    </tbody>
   </table>
  </body>

</html>

脚本.js

var myApp = angular.module('myApp', [])

  .controller('ATableController', function($scope)
  {
      $scope.tableData = [{'col1' : 1,'col2' : 'fff'},
                         {'col1' : 2,'col2' : 'aaa'},
                         {'col1' : 3,'col2' : 'bbb'},
                         {'col1' : 4,'col2' : 'ccc'}];

  })

  .directive('tableSort', [function()
  {
    return {
      restrict : 'A',
      replace  : false,

      scope :
      {
        tableSortData        : '=',
        tableSortRowAccessor : '=',
        tableSortPrimer      : '='
      },

      link : function(scope, element)
      {
        console.log(scope.tableSortData);

        // Await click events on header
        element.find('th').on('click.header', function(event)
        {
          var field = $(event.target).attr('field-name');
          augmentTableSortData();
        });

        function augmentTableSortData()
        {
          console.log(scope);
          console.log(scope.tableSortData);
          scope.tableSortData = [];
          console.log(scope.tableSortData);
        }

      }
    }

  }])

最佳答案

问题是您没有在点击函数中调用 scope.apply:

 function augmentTableSortData()
    {
      console.log(scope);
      console.log(scope.tableSortData);
      scope.tableSortData = [];
      console.log(scope.tableSortData);
      scope.$apply();   // <------ this needs to be called because the scope data is being modified in the click function outside of angular's digest cycle.
    }

演示:http://plnkr.co/edit/as5Wek?p=preview

Documentation :

Scopes provide APIs ($apply) to propagate any model changes through the system into the view from outside of the "Angular realm" (controllers, services, Angular event handlers).

关于javascript - 具有双向绑定(bind)的隔离范围指令不反射(reflect) Controller 范围的变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24945895/

相关文章:

javascript - 使用另一个方法发送到 div 的按钮调用方法

angularjs - 隔离范围内 Angular js 指令中 template 和 templateUrl 的区别

javascript - 大型 AngularJS 应用程序的页面加载性能问题

javascript - 如何关闭使用范围交互后留下的蓝点

javascript - new Backbone.Router.extend ...在没有括号的情况下使用它时的区别

javascript - app.json 文件有什么作用?

javascript - AngularJS 中 Promise 的顺序和时间

javascript - jQuery - ng-view 不运行 JavaScript,除非脚本位于渲染的 html 中

javascript - 如何从 Angular 中的其他函数访问工厂函数

javascript - ng-bind-html 未按预期工作