【问题标题】:AngularJS - Refresh partial viewsAngularJS - 刷新部分视图
【发布时间】:2014-12-08 04:40:11
【问题描述】:

我有一个由部分视图组成的单页应用程序。我想在不重新加载页面的情况下刷新点击元素的部分视图。最好的方法是什么?

【问题讨论】:

  • 你能给我们一个代码示例吗?

标签: angularjs partial-views


【解决方案1】:

我认为 Aliz 在您的答案下方的评论将是每个人都在问的问题,但我会尝试回答您的问题,假设下面的示例代码类似于您要存档的内容。

<section controller='ctrl'>
   <span>{{ text }}</span>
   <button ng-click="update()">update text</button
</section>





angular.module('myApp')
.controller('ctrl', ['$scope', '$timeout', function($scope, $timeout){
   // the data inside $scope.text can be dynamically populated from an AJAX call result. (doesn't matter where its from)
   $scope.text = 'hello world!';

   $scope.update = function() {
      // sometimes this doesn't work and you need to call $scope.$apply below to forcefully update the view.
      $scope.text = 'new text';

      $timeout(function(){
         $scope.$apply();
      }, 0);
   }
}];

如果这不是您要归档的内容,请发布您的代码以获得进一步的帮助。

【讨论】:

  • 为什么是 $timeout?你能帮忙吗?
猜你喜欢
  • 2012-08-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-07
  • 2013-07-20
  • 2018-04-26
  • 2019-01-10
相关资源
最近更新 更多