问题 一次绑定:更新模型并重新渲染视图


我想知道是否可能,使用角度一次性绑定,在模型更新后完全重新渲染视图/模板,也可以通过重新编译模板。 例如,在按下按钮时,可能在反应方式中工作:我更新模型并明确强制更新视图。 基本上这就是我想要实现的目标:

// controller
angular.module('app', []).controller('AppCtrl', function($scope) {
    $scope.items = [
        {id: 1},
        {id: 2},
        {id: 3}
    ];

    $scope.addAndRefresh = function() {
        $scope.items.push({id: 4});
        // manually call render logic here???
    };
});


<!-- HTML template -->
<div ng-repeat="item in ::items">
    {{item.id}}
</div>
<button ng-click="addAndRefresh()">Add</button>

通过单击“添加”按钮,我想刷新视图以查看新添加的项目。


10743
2017-07-07 14:14


起源

为什么要完全重新渲染它,而不仅仅是依赖于修改数据的部分? - Roy Dictus
这是我同样的问题:无论数据如何,我都非常了解如何以及如果可能的话,手动重新渲染一次绑定模板。 - Matteo Piazza
我担心Stack Overflow不是这类问题的论坛。在这里,您需要显示您尝试过的代码,并解释您正在做什么以及为什么,以便人们可以帮助您。 - Roy Dictus
Bizzarre:我一直认为stackoverflow是这个问题的正确位置。我有一个明确的问题(在我看来)我不知道如何处理。也许比我更聪明的人可以提供帮助! - Matteo Piazza
@MatteoPiazza检查我的答案,找出几个问题的解决方案。没有内置到角度核心,但它可能会接近你直到Angular 2.0。 - Kasper Lewau


答案:


我试图找出一些方法来优雅地做到这一点。我希望框架内置一些内容来刷新一次性绑定。我想出的就是使用ngIf删除我想要刷新的元素并将其添加回去。

这是一个演示。单击“添加项目”按钮,您将看到列表的内容  刷新对重复的一次性绑定。检查刷新值并再次单击,项目将更新:

var app = angular.module('demo', []);

app.controller('RefreshCtrl', function($scope, $timeout) {
  var counter = 4;

  $scope.visible = true;

  $scope.items = ['Item1', 'Item2', 'Item3'];

  $scope.addItem = function() {

    if ($scope.refresh) {
      $scope.visible = false;
    }

    $scope.items.push('Item' + counter);

    counter++;

    $timeout(function() {
      $scope.visible = true;
    });
  };

});
<script src="https://code.angularjs.org/1.3.17/angular.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">

<div ng-app="demo" ng-controller="RefreshCtrl" class="container">
  <button class="btn btn-default" ng-click="addItem()">Add Item</button>
  <input type="checkbox" ng-model="refresh" />Refresh Values
  <div ng-if="visible">
    <h3 ng-repeat="item in ::items">{{item}}</h3>
  </div>

  <p>Items Array: {{items}}</p>
</div>


9
2017-07-08 13:29



谢谢,这也是我一直在探索的方式,但是,和你一样,我也在寻找一种优雅的方式。 - Matteo Piazza
谢谢你的解决方案。有趣的是它只适用于你使用的时候 $timeout Angular的功能。使用原生JS setTimeout() 功能(我最初尝试过)不起作用 - 然后是 visible 视图无法识别更改。 - Mathias Conradt
@MathiasConradt是因为setTimeout没有启动新的摘要周期。循环遍历所有手表(包括ng-if)并刷新/重新编译所有内容。 - Nurbol Alpysbayev


根据您的目标,我建议使用以下两种解决方案之一:

我是前者的作者,它与其他解决方案的最大区别在于可以选择挂钩 $parse 服务。

因此,您可以使用介绍 {{:refreshkey:expression}}/:refreshkey:expression 大多数语法 (如果不是全部) Angular中接受表达式的区域。


在您的情况下,实现可能看起来像这样:

JS

angular.module('app', []).controller('AppCtrl', function($scope) {
  $scope.items = [
      {id: 1},
      {id: 2},
      {id: 3}
  ];

  $scope.addAndRefresh = function() {
      $scope.items.push({id: 4});
      /**
       * '$$rebind' is the internal namespace used by angular-bind-notifier.
       * 'refresh' is the refresh key used in your view.
       */
      $scope.$broadcast('$$rebind:refresh');
  };
});

标记

<!-- HTML template -->
<div ng-repeat="item in :refresh:items">
    {{::item.id}}
</div>
<button ng-click="addAndRefresh()">Add</button>

或者,如果你想要一些半动态的东西

JS

angular.module('app', []).controller('AppCtrl', function($scope) {
  $scope.items = [
      {id: 1},
      {id: 2},
      {id: 3}
  ];

  $scope.add = function() {
      $scope.items.push({id: 4});
  };
});

标记

<!-- HTML template -->
<div bind-notifier="{ refresh: items.length }">
  <div ng-repeat="item in :refresh:items">
      {{::item.id}}
  </div>
</div>
<button ng-click="add()">Add</button>

看看 自述 和这个 jsBin 一些用法示例。


5
2017-07-13 15:56



小心kcd-recompile。我最近发现在重新编译时不会销毁子组件范围。在长时间的会话和几次重新编译过程中,这将使事情陷入困境。 github.com/kentcdodds/kcd-angular/issues/8 - Devon Sams