一下提供3中load加载的写法:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>angular中onload事件</title>
</head>
<body>
<div ng-app="dome">
<div ng-controller="loadevent">
<span ng-bind=apple></span>
</div>
<div ng-controller="test">
<div data-ng-init="load()" >
<span ng-bind=testtext>测试文字</span>
</div>
</div>
</div>
<script type="text/javascript" src="angular.js"></script>
<script type="text/javascript">
// 1.直接写在js中 不建议这样使用
angular.element(window).bind('load', function() {
alert('1');
});
// 2.在controller里面利用$watch调用$viewContentLoaded事件
angular.module("dome",["ng"]).controller("loadevent",function($scope){
$scope.$watch('$viewContentLoaded', function() {
$scope.apple = "iphone8 X"
});
// 3.利用data-ng-init 注意:data-ng-init在controller里面才会启作用
}).controller('test', function($scope) {
$scope.load = function() {
$scope.testtext = "code in here"
}
});
</script>
</body>
</html>