$ location.url和$ scope。$ apply =无法读取属性'$$ nextSibling'的null

时间:2022-06-12 19:40:19

We have a service handler that changes the location. The location does not change until something else triggers a digest. So, I used $apply, but getting errors from that.

我们有一个更改位置的服务处理程序。在其他内容触发摘要之前,位置不会更改。所以,我使用了$ apply,但是从中获得了错误。

$scope.myFunction = function () {
    $scope.$apply(function () {
        $location.url(path);
    });
};

Above gives this error:

上面给出了这个错误:

Uncaught TypeError: Cannot read property '$$nextSibling' of null

From what I found, it seems like the $scope is getting destroyed during a phase cycle...But how can you use apply() on a location change without this error?

从我发现的情况来看,似乎$ scope在一个阶段循环中被破坏了...但是如何在没有此错误的情况下对位置更改使用apply()?

EDIT:

Testing to see if this scenario is related to our current app and discovered that reloading the route works oddly enough, which seems like a hack?

测试看看这个场景是否与我们当前的应用程序有关,并发现重新加载路由的工作很奇怪,这似乎是一个黑客攻击?

$scope.myFunction = function () {
   $location.url(path);
   $route.reload() 
};

1 个解决方案

#1


2  

Inject a $rootScope and use it instead of the $scope:

注入$ rootScope并使用它而不是$ scope:

$location.url(path);
$rootScope.$apply();

The $rootScope will be available for the lifetime of the app.

$ rootScope将在应用程序的生命周期内可用。

#1


2  

Inject a $rootScope and use it instead of the $scope:

注入$ rootScope并使用它而不是$ scope:

$location.url(path);
$rootScope.$apply();

The $rootScope will be available for the lifetime of the app.

$ rootScope将在应用程序的生命周期内可用。