Call AngularJS function on document ready

后端 未结 2 1213
无人及你
无人及你 2020-12-23 14:40

Is there a way to call an Angular function from a JavaScript function?

function AngularCtrl($scope) {
  $scope.setUserName = function(student){  
    $scope.         


        
2条回答
  •  难免孤独
    2020-12-23 15:03

    Angular has its own function to test on document ready. You could do a manual bootstrap and then set the username:

    angular.element(document).ready(function () {
        var $injector = angular.bootstrap(document, ['myApp']);
        var $controller = $injector.get('$controller');
        var AngularCtrl = $controller('AngularCtrl');
        AngularCtrl.setUserName();
    });
    

    For this to work you need to remove the ng-app directive from the html.

提交回复
热议问题