e起飞young
一个前端码农的技术博客
angularjs实现类似window.onload()或$(document).ready()监听页面加载完成事件的几种方法
[ 2018/01/22 ]

1.html中直接写(不建议)

<script type="text/javascript">
    angular.element(window).bind('load', function() {
        alert('1');
    });
    alert('2');
</script>

2.在controller里面利用$on或者$watch

(function(){"use strict";
	angular.module('myApp')
	.controller('testSth', ['$scope', function($scope){
		$scope.$on('$viewContentLoaded', function(){
			console.log('1');
		});
		console.log('2');
	}]);
})();
//用$watch
(function(){"use strict";
	angular.module('myApp')
	.controller('testSth', ['$scope', function($scope){
		$scope.$watch('$viewContentLoaded', function(){
			console.log('1');
		});
		console.log('2');
	}]);
})();

3.利用ng-init

<div ng-controller="testSth">
     <div ng-init="loadSth()" ></div>
</div>
(function(){"use strict";
	angular.module('myApp')
	.controller('testSth', ['$scope', function($scope){
		$scope.loadSth = function(){
			console.log('i am loaded....')
		}
	}]);
})();

有朋自远方来...评论一下呗O(∩_∩)O

  • 搜索