Angularjs 实现 $(document).ready()的两种方法

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

bookControllers.controller('bookctrl_test', ['$scope', '$routeParams',
function($scope, $routeParams) {
$scope.$on('$viewContentLoaded', function() {
alert('1');
});
alert('2');
}]);

bookControllers.controller('bookctrl_test1', ['$scope', '$routeParams',
function($scope, $routeParams) {
$scope.$watch('$viewContentLoaded', function() {
alert('1');
});
alert('2');
}]);


2.利用data-ng-init
<div ng-controller="test">
<div data-ng-init="load()" ></div>
</div>
注意:data-ng-init在controller里面才会启作用
bookControllers.controller('testInit', ['$scope', '$routeParams',
function($scope, $routeParams) {
$scope.load = function() {
alert('code here');
}
}]);

原文地址:https://www.cnblogs.com/xiaxianfei/p/5275784.html