[英]How to call an angularJS function from javascript?
I need to call an angularJS
function from a javascript
function.我需要从
javascript
angularJS
。 This is my javascript
function:这是我的
javascript
function:
function callAngularFunction(userid, serviceid, system) {
angular.element(document.getElementById('myServiceId')).scope().test();
}
This is my controller code:这是我的 controller 代码:
serviceApp.controller('myService', function ($scope, $location, $window) {
$scope.test = function () {
console.log("hello from test");
}
});
And this is how I included it in html
:这就是我将它包含在
html
中的方式:
<body ng-app="serviceApp" ng-controller="myService" id="myServiceId">
Whenever I call callAngularFunction
, I get this error:每当我调用
callAngularFunction
时,都会收到此错误:
TypeError: Cannot read property 'test' of undefined
TypeError:无法读取未定义的属性“测试”
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
检查您是否在正文标记
</body>
的末尾导入脚本,也许由于这个原因,它在您的代码中不可用,id myServiceId
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.