使用jQuery / javascript调用angularjs功能 [英] Call angularjs function using jquery/javascript
问题描述
我试图使用javascript / jQuery的调用函数的角度,以下是我发现的链接
I am trying to call angular function using javascript/jQuery, following are the link I found
- Call Angular Function with Jquery
- Call controller function from outside of controller component
我已经在浏览器控制台相同的步骤,但得到的错误
I have taken the same steps but getting error in browser console
Uncaught TypeError: Cannot call method 'myfunction' of undefined
我创建了小提琴。如何调用myfunction的函数,并显示警报?任何想法?
I have created the fiddle. How to call myfunction function and alert is displayed? Any Idea?
推荐答案
的解决方案中,您链接是正确的问题提供。的与您的实现问题是,你有没有指定的 ID 元素的正确。
Solution provide in the questions which you linked is correct. Problem with your implementation is that You have not specified the ID of element correctly.
其次,你需要使用负荷
事件来执行你的code。目前,DOM没有加载元素,因此没有发现这样你得到错误。
Secondly you need to use load
event to execute your code. Currently DOM is not loaded hence element is not found thus you are getting error.
的 HTML 的
<div id="YourElementId" ng-app='MyModule' ng-controller="MyController">
Hi
</div>
的 JS code 的
angular.module('MyModule', [])
.controller('MyController', function ($scope) {
$scope.myfunction = function (data) {
alert("---" + data);
};
});
window.onload = function () {
angular.element(document.getElementById('YourElementId')).scope().myfunction('test');
}
这篇关于使用jQuery / javascript调用angularjs功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!