Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
68
rated 0 times [  75] [ 7]  / answers: 1 / hits: 72550  / 12 Years ago, wed, december 12, 2012, 12:00:00

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



function AngularCtrl($scope) {
$scope.setUserName = function(student){
$scope.user_name = 'John';
}
}


I need the following functionality in my HTML:



jQuery(document).ready(function(){
AngularCtrl.setUserName();
}


The problem here is my HTML code is present when page is loaded and hence the ng directives in the html are not compiled. So I would like to $compile(jQuery(PopupID)); when the DOM is loaded.



Is there a way to call a Angular function on document ready?


More From » angularjs

 Answers
34

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.


[#81475] Monday, December 10, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
zaynerogerb

Total Points: 454
Total Questions: 109
Total Answers: 97

Location: Comoros
Member since Tue, Mar 14, 2023
1 Year ago
zaynerogerb questions
;