angular中實現jQuery的Document Ready


angular中不推薦混用JQuery的,原因呢問度娘。

其實這是一個比較蛋疼的問題,尤其是angular2.0,盡量不要在頁面上寫js,用ts寫到模塊里面去吧。。

汲取各位先人的智慧,還是列一下

window.onload(),$(document).ready()在angular中要怎么寫:

1,html中直接寫(請自行引用angular.js,否則2.0的頁面中是不帶有angular對象的)

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

不建議,直接在模板里面,寫js代碼。

2,在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');
}]);

3,利用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');
        }
}]);

好了,去學學TypeScript(ts)的語法吧!

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM