Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/379.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript AngularJS document.ready不';使用ng视图时不工作_Javascript_Angularjs - Fatal编程技术网

Javascript AngularJS document.ready不';使用ng视图时不工作

Javascript AngularJS document.ready不';使用ng视图时不工作,javascript,angularjs,Javascript,Angularjs,在应用程序中的多条路线之间导航时,angularJS中的document.ready出现问题。它只在我使用ctrl+f5(页面重新加载)时工作;在页面之间导航似乎不会将文档的状态更改为就绪 控制器 angular.element(document).ready(function() { window.scrollTo(0,90); }); 主html文件 <!DOCTYPE html > <html ng-app="myApp"> <head&

在应用程序中的多条路线之间导航时,angularJS中的document.ready出现问题。它只在我使用ctrl+f5(页面重新加载)时工作;在页面之间导航似乎不会将文档的状态更改为就绪

控制器

  angular.element(document).ready(function() {
    window.scrollTo(0,90);
});
主html文件

<!DOCTYPE html >
<html ng-app="myApp">
    <head>
        <meta http-equiv="content-type" content="text/html;charset=utf-8" />
        <title></title>
    </head>
    <body>
        <div class="container">
            <div ng-view></div>
        </div>
    </body>
</html>

您可以在路由中定义的控制器中进行监听,例如,
SomeController
SomeRouteController
$viewContentLoaded
在每次重新加载ngView内容时都会发出,并且应该提供与
文档类似的功能。在angularjs中发送时,ready

function SomeController($scope) {
   $scope.$on('$viewContentLoaded', function() {window.scrollTo(0,90);});
}

加载
index.html
时,
document.ready
也只会触发一次。加载路由配置中定义的部分模板时不会触发该事件。

我可以通过Dave的回答和使用


扩展@davekr的答案,我发现我需要添加$timeout以确保摘要已完成,并且html元素可供查询:

function SomeController($scope) {
    $scope.$on('$viewContentLoaded', function() {
        $timeout(function(){
            //Do your stuff
        });
    });
}

我尝试了许多其他事件,这是唯一可靠工作的方法。

您是否尝试过jqLite的native
bind
方法?像
angular.element(document).bind(“ready”,function(){…
?不清楚您如何在“页面”之间导航(这些
ng视图是
s、
ng包含
s完整文档请求,…)但一般来说,您不需要使用Angular绑定到
DOMContentLoaded
,而是创建一个.tnx@AndréDion。我在模块配置中使用ng视图,并在('/',{controller:SomeController,templateUrl:'somehtml.html')中使用路由.tnx。我使用您的解决方案,并使用firebug对其进行调试。它会将页面滚动到该点,但在页面完全加载后,会将某些内容重置,并滚动到页面顶部page@SoheilGh可能尝试将侦听器更改为此
函数(事件){event.preventDefault();window.scrollTo(0,90);}
tnx您。我使用firebug找出哪个事件页面滚动到顶部,然后找到它。我将事件更改为$routeChangeSuccess,它终于可以工作了
function SomeController($scope) {
    $scope.$on('$routeChangeSuccess', function() {window.scrollTo(0,90);});
}
function SomeController($scope) {
    $scope.$on('$viewContentLoaded', function() {
        $timeout(function(){
            //Do your stuff
        });
    });
}