简体   繁体   English

$ viewContentLoaded没有触发

[英]$viewContentLoaded isn't firing

$viewContentLoaded never fires in my Angular controller. $viewContentLoaded永远不会在我的Angular控制器中触发。 The following: 下列:

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? 什么会导致这个?

Try this instead: 试试这个:

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

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 $viewContentLoaded is emitted from the ng-view scope. 我不确定ng-view标签中的内容是什么,但$viewContentLoaded是从ng-view范围发出的。 This means, it only goes up from the ng-view and thus your controller would never catch it. 这意味着,它只会从ng-view上升,因此你的控制器永远不会捕获它。

I solved this by specifying the controller in the app.js route provider rather than using ng-controller in a div in the view. 我通过在app.js路由提供程序中指定控制器而不是在视图中的div中使用ng-controller来解决这个问题。 I only use this method if I need to use $viewContentLoaded, otherwise I use ng-controller. 如果我需要使用$ viewContentLoaded,我只使用此方法,否则我使用ng-controller。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM