javascript - Angularjs app.run 未加载

标签 javascript angularjs

我的 app.run() 遇到问题。在主页中 app.run() 正在工作。当我单击菜单按钮“事件”并且在页面中 events.html app.run() 不起作用。但是当我重新加载此页面时,app.run() 正在工作。我不知道该怎么办。页面main.html和events.html在ngView中,菜单也在ngView中。
Controller .js

afroEarthApp.config(['$routeProvider', function($routeProvider){
    $routeProvider
      .when('/',{
        templateUrl:'template/home.html',
        controller:'afroEarthMainCtrl'
      })
      .when('/events',{
        templateUrl:'template/events.html',
        controller:'eventsCtrl'
      })
      .when('/sites/:niche', {
        templateUrl:'template/single.html',
        controller:'SingleCtrl'
      })
      .otherwise({
        redirectTo: '/'
      })
}]);
afroEarthApp.controller('afroEarthMainCtrl',['$scope','$http','$location','$cookies', '$rootScope', '$route', function($scope, $http, $location, $cookies, $rootScope, $route) {
...some code...
}]);

afroEarthApp.controller('eventsCtrl',['$scope','$http','$location','$cookies', '$rootScope', '$route', function($scope, $http, $location, $cookies, $rootScope, $route) {
...some code...
}]);

afroEarthApp.run(['$log', '$rootScope', '$route', function ($log, $rootScope, $route) {
    $rootScope.$on('$viewContentLoaded', function ($rootScope, $route) {
...some code...
})
}]);

home.html

        <ul class="nav navbar-nav navbar-right">
            <li><a class="page-scroll" href="#websites">Lifestyle sites</a></li>
            <li><a class="page-scroll" href="stories">Success Stories</a></li>
            <li><a href="#/events" data-target="#events" target="_self">Events</a></li>
            <li><a class="page-scroll" ng-href="http://{{singleNiche.url}}.afroearth.com/login/">Login</a></li>
            <li class="other-countries">
                <a href="#">
                    {{singleNiche.country}}<img src="{{singleNiche.countryFlag}}"><i class="icon-caret-down"></i>
                </a>
                <ul>
                    <li ng-repeat="country in singleNiche.countries"><a ng-click="setCountry(country.url)" >{{country.name}}</a></li>
                </ul>
            </li>
        </ul>

你能帮我一下吗?

最佳答案

在您的app.run中添加以下内容

 $rootScope.$on('$locationChangeStart', function (event, nextLocation, currentLocation) {
//your code here
});

每次 url 更改时都会调用此函数。

关于javascript - Angularjs app.run 未加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36476876/

相关文章:

javascript - 哪些浏览器支持 ECMAScript 6 的导入和导出语法?

javascript - Froala WYSIWYG 编辑器未出现在浏览器中

javascript - 查询。仅将功能应用于关联的内容/div

angularjs - Karma 测试未找到缓存模板

javascript - 使用 Angular 服务后 undefined object

javascript - 显示本地用户文件并显示内容

javascript - 绘图.io : How can I debug unminified js code?

javascript - $q.all 用于动态数字或 promise

javascript - Angularjs 多选 : Cannot read property 'length' of undefined

javascript - 无法更新指令模型