How to call an angularJS function from javascript?

105 Views Asked by At

I need to call an angularJS function from a javascript function. This is my javascript function:

function callAngularFunction(userid, serviceid, system) {

    angular.element(document.getElementById('myServiceId')).scope().test();

}

This is my controller code:

serviceApp.controller('myService', function ($scope,  $location, $window) {
    $scope.test = function () {
        console.log("hello from test");
    }
});

And this is how I included it in html:

<body ng-app="serviceApp" ng-controller="myService" id="myServiceId">

Whenever I call callAngularFunction, I get this error:

TypeError: Cannot read property 'test' of undefined

1

There are 1 best solutions below

0
Luis Gallegos On

Check if you are importing the scripts in the end of the body tag </body> maybe for that reason it is not available in your code the id myServiceId