AngularJS:$ viewContentLoaded在部分视图出现之前触发
对于局部视图,我想做一些我通常用$(document).ready(function() {...})
做的JavaScript,比如将venet监听器绑定到元素。 我知道这对AngularJS和加载到“根”视图中的部分视图不起作用。
因此,我向侦听器添加了一个侦听器,侦听器监听$viewContentLoaded
事件。 调用侦听器的函数,因此事件被触发但在我看来好像是在呈现局部视图之前。 当我在侦听器的函数中设置断点并使用firebug调试它时,我也没有看到元素,函数中的jquery选择也没有找到局部视图的元素。
这就是控制器的样子:
angular.module('docinvoiceClientAngularjsApp') .controller('LoginController', function ($scope, $rootScope) { $scope.$on('$viewContentLoaded', function(event) { console.log("content loaded"); console.log($("#loginForm")); // breakpoint here }); [...]
我想我做错了,因为如果这是一个常见的错误,必须在stackoverflow上发布更多post。
当我使用ui-router和ui-view时 ,我会给你一个路由文件的摘录:
angular .module('docinvoiceClientAngularjsApp', [ 'ui.router', 'ngAnimate', 'ngCookies', 'ngResource', 'ngMessages', 'ngRoute', 'ngSanitize', 'ngTouch' ]) .config(function ($routeProvider, $stateProvider) { $stateProvider .state('login', { url: '/', templateUrl: 'components/login/loginView.html', controller: 'LoginController' }) .run(['$state', function ($state) { $state.transitionTo('login'); }]) [...]
任何帮助表示赞赏。 谢谢和亲切的问候
更新1:我将错误删除到以下用例:loginView.html如下所示:
Interesting Posts