jQuery $viewContentLoaded 没有触发

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16223819/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 16:44:00  来源:igfitidea点击:

$viewContentLoaded isn't firing

javascriptjqueryangularjs

提问by Mike Pateras

$viewContentLoadednever fires in my Angular controller. The following:

$viewContentLoaded永远不会在我的 Angular 控制器中触发。下列:

function MyController($scope)
{
    $scope.$on('$viewContentLoaded', function()
    {
        alert("blah");
    });
}

Never hits a breakpoint, and never pops up the alert. I think this is pretty standard use, so I'm not sure what it could be, but it's definitely not firing. Everything else in the controller is behaving properly.

永远不会遇到断点,也永远不会弹出警报。我认为这是非常标准的用法,所以我不确定它可能是什么,但它绝对不会触发。控制器中的其他一切都运行正常。

What would cause this?

什么会导致这种情况?

采纳答案by dnc253

Base on your comment above, it sounds like you have something like this:

根据你上面的评论,听起来你有这样的事情:

<ng-view>
   <div ng-controller="MyController"></div>
</ng-view>

I'm not sure what content in the ng-view tag will do, but the $viewContentLoadedis emittedfrom the ng-view scope. This means, it only goes up from the ng-viewand thus your controller would never catch it.

我不知道在NG-view标记会做什么样的内容,但$viewContentLoaded发射从NG-视图范围。这意味着,它只会从 上升ng-view,因此您的控制器永远不会抓住它。

回答by CodeOverRide

Try this instead:

试试这个:

  function MyController($scope)
    {
        $scope.$watch('$viewContentLoaded', function()
        {
            alert("blah");
        });
    }

回答by Jim Clouse

I solved this by specifying the controller in the app.js route provider rather than using ng-controller in a div in the view. I only use this method if I need to use $viewContentLoaded, otherwise I use ng-controller.

我通过在 app.js 路由提供程序中指定控制器而不是在视图的 div 中使用 ng-controller 解决了这个问题。我只在需要使用 $viewContentLoaded 时才使用这种方法,否则我使用 ng-controller。

angular.module('myapp', [...])
.config(function ($routeProvider, $locationProvider) {
   $routeProvider
      .when('/MyRoute', {templateUrl: 'views/MyView.html', controller: MyController})
  });