路线更改时刷新页面没有无限循环

时间:2018-02-12 08:13:07

标签: javascript angularjs

我正在尝试刷新AngularJS加载的每个新页面,如下所示:

控制器:

angular.module('adsomaApp')
  .controller('BrandsController', function ($window) {
    var vm = this;
    vm.init = function() {
      $window.location.reload();
    };
  });

查看

<div class="container" ng-controller="BrandsController as vm" ng-init="vm.init()">Test</div>

奇怪的是,这会导致页面无限期重新加载。它陷入了循环。知道如何在路线更换时重新加载一次吗?

我认为问题的原因在于:

'use strict';

/**
 * @ngdoc directive
 * @name adsomaApp.directive:resize
 * @description
 * # resize
 */
angular.module('adsomaApp')
  .directive('resize', function () {
    return function (scope, $window) {
      var w = angular.element($window);

      scope.getWindowDimensions = function () {
        return {
          'h': screen.height,
          'w': window.innerWidth
        };
      };

      scope.$watch(scope.getWindowDimensions, function (newValue) {
        scope.windowHeight = newValue.h;
        scope.windowWidth = newValue.w;

        scope.youtubeBgVideoStyle = function () {
          return {
            'max-width': '1000%', 
            'margin-left': '0px', 
            'width': newValue.w + 'px',
            'min-height': '100vh', 
            'height': newValue.h + 'px',
            'margin-bottom': '-50%'
          };
        };
      }, true);

      w.bind('resize', function () {
        scope.$apply();
      });

      w.bind('load', function () {
        scope.$apply();
      });
    };
  });

如何在路线更改时触发此指令刷新?

0 个答案:

没有答案