使用ui.router呈现的页面的Angular JS事件

时间:2013-11-22 15:25:03

标签: javascript angularjs angular-ui-router

背景情景:

我有ng-repeat填充我的视图,动态扩大<div>容器。

我需要在渲染完成后调整<div>的高度,我还需要在我的应用的每个其他页面上执行此操作。

我很清楚ready()功能:

    angular.element(document).ready(function() {
        console.log('ready')
    })

但是这个放在app.js中的代码只能在第一页打开(或重新加载)时执行。

<小时/> 重要提示:
我正在使用ui.router来浏览我的“页面”,所以每当我需要更改页面时,我都会调用`$ state.transitionTo()'。


问题:每当网页完全呈现时,是否会播放广角事件?

2 个答案:

答案 0 :(得分:14)

在寻找解决方案时,我发现只要DOM完全呈现(在每次新的状态转换时)都会调用ui.router事件

事件为$viewContentLoaded,语法非常简单:

    $rootScope.$on('$viewContentLoaded', function (event) {
            console.log('lock & loaded')
    })


我认为这可能对其他人有用,
干杯

答案 1 :(得分:0)

您也可以像这样使用onEnter for uirouter,

$stateProvider
    .state('printer',
    {
        url: "/printer",
        views:
        {
            "view":
            {
                templateUrl: "/static/tpls/cloud/app/printer.php"
            }
        },
        onEnter: function(){
            try {
                window.webkit.messageHandlers.pageIn.postMessage("enter printer page");
            } catch(err) {
                console.log('The native context does not exist yet');
            }
        },
        onExit: function(){
            try {
                window.webkit.messageHandlers.pageOut.postMessage("leave printer page");
            } catch(err) {
                console.log('The native context does not exist yet');
            }
            document.getElementById('liveMovie').style.height='30px';
        }
    })
相关问题