重新初始化Angular.js控制器 [英] Reinitialize an Angular.js controller

查看:212
本文介绍了重新初始化Angular.js控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果你有一个控制器来操作Angular.js $范围内的变量,有一个惯用的方式:

if you have a controller to manipulate $scope variables in Angular.js, is there an idiomatic way to:


  • 复位控制器的$范围,

  • 重启控制器初始化?

对于复杂的控制器,这将是非常方便的不要有每一个变量恢复到它的初始值,特别是如果你真正想要的是控制器和范围的重新初始化简单。通过 $ location.path()没有帮助,但再次浏览相同的URL

For complex controllers it would be very convenient not to have to reset every variable to it's initial value, especially if what you really want is a simple reinitialization of the controller and the scope. Navigating to the same URL again via $location.path() doesn't help, though.

编辑:假设我不能使用任何 $ window.location的破解,因为这会违反CSP在Chrome封装应用

Suppose I can't use any $window.location hack because this would violate the CSP in Chrome Packaged Apps.

推荐答案

就问,我终于找到了解决这个使用的 $ route.reload()

Just after asking, I finally found one way to solve this using $route.reload().

myapp.Controller('SampleController', function($location, $route) {

  $scope.navTo = function(url) {
    if ($location.path() === url) {
      $route.reload();
    } else {
      $location.path(url);
    }
  }

});

我还在想,这一定是有更优雅的解决方案,但是这肯定对我的作品。

I'm still thinking, that there must be some more elegant solution, but this definitely works for me.

这篇关于重新初始化Angular.js控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆